本文介绍了如何添加花药“入口"进入这个JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

插入

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

进入

{活动":{1":{"campaign_id": "1","title": "Nike Air 2015 广告",描述":空,path_to_logo":空,"开始时间": "09/11/2015 22:42:08","end_time": "09/03/2016 22:42:08",暂停":0",目的地":{1":{"destination_id": "1","campaign_id": "1","url": "www.nike.com/nike_air","description": "耐克航空目的地",连接":{3":{"connection_id": "3","destination_id": "1","tag_id": "0","国家": "苏格兰","county": "约克郡","town": "东火腿","post_code": "SE1 1AA","custom": "公交车站","描述": "连接号 3"}}},2":{"destination_id": "2","campaign_id": "1","url": "www.nike.com/nike_air/sub_campaign","description": "耐克空气-免费鞋带促销目的地",连接":{2":{"connection_id": "2","destination_id": "2","tag_id": "0","country": "英格兰","county": "德文","town": "东火腿","post_code": "SE1 1AA","custom": "公交车站","描述": "连接号 2"},4":{"connection_id": "4","destination_id": "2","tag_id": "0","country": "英格兰","county": "约克郡","town": "Felixswtowe","post_code": "RB3 9YR","custom": "警察静电","description": "连接号 4"},6":{"connection_id": "6","destination_id": "2","tag_id": "0","国家": "苏格兰","county": "埃塞克斯","town": "约克","post_code": "JD8 4LF","custom": "别处","description": "连接号 6"},9":{"connection_id": "9","destination_id": "2","tag_id": "0","国家": "苏格兰","county": "科克","town": "约克","post_code": "JD8 4LF","custom": "在女士厕所里","描述": "连接编号 9"}}}}},2":{"campaign_id": "2","title": "耐克足球鞋运动",描述":空,path_to_logo":空,"开始时间": "09/12/2015 22:42:08","end_time": "09/01/2016 22:42:08",暂停":0",目的地":{3":{"destination_id": "3","campaign_id": "2","url": "www.nike.com/nike_football_boots/","description": "耐克足球鞋目的地",连接":{}},4":{"destination_id": "4","campaign_id": "2","url": "www.nike.com/nike_football_boots/sub_campaign","description": "购买左引导,获得右引导免费目的地",连接":{}}}},3":{"campaign_id": "3","title": "耐克总推广活动",描述":空,path_to_logo":空,"start_time": "09/12/2013 22:42:08","end_time": "09/08/2016 22:42:08",暂停":0",目的地":{5":{"destination_id": "5","campaign_id": "3","url": "www.nike.com/general_promotion","description": "耐克一般推广目的地",连接":{}},6":{"destination_id": "6","campaign_id": "3","url": "www.nike.com/general_promotion/discount_coupon","description": "20% off 优惠券目的地",连接":{}}}}}}
解决方案

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

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

将空营销活动添加到营销活动对象.显然,您需要事先定义 date.

obj.campaigns[nextId] = {'标题': '','描述': '','path_to_logo': '',开始时间":日期,'end_time':日期,'暂停':假}

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": {}
                }
            }
        }
    }
}
解决方案

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
}

这篇关于如何添加花药“入口"进入这个JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:05