How to add anther “entry” into this JSON?

心已入冬 提交于 2020-01-06 20:01:59

问题


I downloaded some JSON, as shown below. In the developer console it looks like an object containing nested objects which contain nested objects ...

I would like to add a new, empty "campaign" into the data (at the front). How do I do that?

Insert

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

into

{
    "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": {}
                }
            }
        }
    }
}

回答1:


Work out what the next campaign id should be:

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

Add the empty campaign to the campaigns object. Obviously you'll need to define date beforehand.

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


来源:https://stackoverflow.com/questions/34190454/how-to-add-anther-entry-into-this-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!