【问题标题】:How to add anther "entry" into this JSON?如何在这个 JSON 中添加另一个“条目”?
【发布时间】:2015-12-09 22:46:38
【问题描述】:

我下载了一些 JSON,如下所示。在开发者控制台中,它看起来像一个包含嵌套对象的对象,其中包含嵌套对象...

我想在数据中添加一个新的空“活动”(在前面)。我该怎么做?

插入

var blankCampaignData = {'title': '', 'description': '', 'path_to_logo': '', 'start_time': date, 'end_time' : date, 'paused': false};

进入

{
    "campaigns": {
        "1": {
            "campaign_id": "1",
            "title": "Nike Air 2015 campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/11/2015 22:42:08",
            "end_time": "09/03/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "1": {
                    "destination_id": "1",
                    "campaign_id": "1",
                    "url": "www.nike.com/nike_air",
                    "description": "Nike air destination",
                    "connections": {
                        "3": {
                            "connection_id": "3",
                            "destination_id": "1",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Yorkshire",
                            "town": "East Ham",
                            "post_code": "SE1 1AA",
                            "custom": "bus stop",
                            "description": "Connection number 3"
                        }
                    }
                },
                "2": {
                    "destination_id": "2",
                    "campaign_id": "1",
                    "url": "www.nike.com/nike_air/sub_campaign",
                    "description": "Nike air - free laces promotion destination",
                    "connections": {
                        "2": {
                            "connection_id": "2",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Engerland",
                            "county": "Devon",
                            "town": "East Ham",
                            "post_code": "SE1 1AA",
                            "custom": "bus stop",
                            "description": "Connection number 2"
                        },
                        "4": {
                            "connection_id": "4",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Engerland",
                            "county": "Yorkshire",
                            "town": "Felixswtowe",
                            "post_code": "RB3 9YR",
                            "custom": "police staticon",
                            "description": "Connection number 4"
                        },
                        "6": {
                            "connection_id": "6",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Essex",
                            "town": "York",
                            "post_code": "JD8 4LF",
                            "custom": "somewhere else",
                            "description": "Connection number 6"
                        },
                        "9": {
                            "connection_id": "9",
                            "destination_id": "2",
                            "tag_id": "0",
                            "country": "Scotland",
                            "county": "Cork",
                            "town": "York",
                            "post_code": "JD8 4LF",
                            "custom": "in the ladies' loo",
                            "description": "Connection number 9"
                        }
                    }
                }
            }
        },
        "2": {
            "campaign_id": "2",
            "title": "Nike football boots campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/12/2015 22:42:08",
            "end_time": "09/01/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "3": {
                    "destination_id": "3",
                    "campaign_id": "2",
                    "url": "www.nike.com/nike_football_boots/",
                    "description": "Nike footie boots destination",
                    "connections": {}
                },
                "4": {
                    "destination_id": "4",
                    "campaign_id": "2",
                    "url": "www.nike.com/nike_football_boots/sub_campaign",
                    "description": "Buy left boot, get right boot free destination",
                    "connections": {}
                }
            }
        },
        "3": {
            "campaign_id": "3",
            "title": "Nike general promotion campaign",
            "description": null,
            "path_to_logo": null,
            "start_time": "09/12/2013 22:42:08",
            "end_time": "09/08/2016 22:42:08",
            "paused": "0",
            "destinations": {
                "5": {
                    "destination_id": "5",
                    "campaign_id": "3",
                    "url": "www.nike.com/general_promotion",
                    "description": "Nike general promotion destination",
                    "connections": {}
                },
                "6": {
                    "destination_id": "6",
                    "campaign_id": "3",
                    "url": "www.nike.com/general_promotion/discount_coupon",
                    "description": "20% off coupon destination",
                    "connections": {}
                }
            }
        }
    }
}

【问题讨论】:

标签: javascript json object


【解决方案1】:

确定下一个广告系列 ID 应该是什么:

var nextId = +Object.keys(obj.campaigns).sort().pop() + 1;

将空广告系列添加到广告系列对象。显然你需要事先定义date

obj.campaigns[nextId] = {
  'title': '',
  'description': '',
  'path_to_logo': '',
  'start_time': date,
  'end_time' : date,
  'paused': false
}

【讨论】:

  • 谢谢,我会试试的。顺便说一句,我更喜欢在前面添加它
  • 你不能。对象未排序。如果campaigns 是一个数组,那么您可以将unshift 新对象作为第一个元素。
  • “前面”和“后面”没有意义,因为对象没有排序。使其有意义的唯一方法是将带有键 1 的对象移动到新键并用新对象替换它。
  • 你要么必须调整你的 PHP 以给你一个对象数组,要么按照我的建议将 id=1 的对象移动到一个新对象,然后将新对象分配给关键 1. 从长远来看,听起来更改 PHP 对您来说会更好。
  • 我想知道我是否可以这样做。您的 cmets 促使我进行搜索,我找到了 stackoverflow.com/questions/22408311/return-an-array-of-objects while ($row = $stmt->fetch(PDO::FETCH_OBJ)) 谢谢!!现在我只需要弄清楚如何让 PHP 嵌套树结构(每个活动都有多个目的地,每个目的地都有多个连接),所以我使用 3 个嵌套循环,每个循环都有一个 SELECT 语句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多