将键值对附加到 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14272051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:42:29  来源:igfitidea点击:

Appending a key value pair to a json object

json

提问by Aayush

This is the json objectI am working with

这是我正在使用的json 对象

{
    "name": "John Smith",
    "age": 32,
    "employed": true,
    "address": {
        "street": "701 First Ave.",
        "city": "Sunnyvale, CA 95125",
        "country": "United States"
    },
    "children": [
        {
            "name": "Richard",
            "age": 7
        },
        {
            "name": "Susan",
            "age": 4
        },
        {
            "name": "James",
            "age": 3
        }
    ]
}

I want this as another key-value pair :

我希望这是另一个键值对:

"collegeId": {
                      "eventno": "6062",
                      "eventdesc": "abc"
                                            }; 

I tried concat but that gave me the result with || symbol and I cdnt iterate. I used spilt but that removes only commas.

我试过 concat 但结果是 || 符号,我 cdnt 迭代。我使用了溢出但只删除了逗号。

concattedjson = JSON.stringify(JSON.parse(json1).concat(JSON.parse(json2)));

How do I add a key pair value to an existing json object ? I am working in javascript.

如何将密钥对值添加到现有的 json 对象?我在 JavaScript 中工作。

采纳答案by Jobert Enamno

This is the easiest way and it's working to me.

这是最简单的方法,它对我有用。

var testJson = {
        "name": "John Smith",
        "age": 32,
        "employed": true,
        "address": {
            "street": "701 First Ave.",
            "city": "Sunnyvale, CA 95125",
            "country": "United States"
        },
        "children": [
            {
                "name": "Richard",
                "age": 7
            },
            {
                "name": "Susan",
                "age": 4
            },
            {
                "name": "James",
                "age": 3
            }
        ]
    };
    testJson.collegeId = {"eventno": "6062","eventdesc": "abc"};

回答by Travis J

You need to make an object at reference "collegeId", and then for that object, make two more key value pairs there like this:

您需要在引用“collegeId”处创建一个对象,然后对于该对象,再创建两个键值对,如下所示:

var concattedjson = JSON.parse(json1);
concattedjson["collegeId"] = {};
concattedjson["collegeId"]["eventno"] = "6062";
concattedjson["collegeId"]["eventdesc"] = "abc";

Assuming that concattedjson is your json object. If you only have a string representation you will need to parseit first before you extend it.

假设 concattedjson 是您的 json 对象。如果您只有一个字符串表示,parse则在扩展它之前需要先使用它。

Edit

编辑

demofor those who think this will not work.

demo对于那些认为这行不通的人。

回答by Jonathan M

Just convert the JSON string to an object using JSON.parse()and then add the property. If you need it back into a string, do JSON.stringify().

只需使用将 JSON 字符串转换为对象JSON.parse(),然后添加属性即可。如果您需要将其恢复为字符串,请执行JSON.stringify().

BTW, there's no such thing as a JSON object. There are objects, and there are JSON strings that represent those objects.

顺便说一句,没有 JSON 对象这样的东西。有对象,并且有表示这些对象的 JSON 字符串。

回答by shalonteoh

const newTestJson = JSON.parse(JSON.stringify(testJson));
newTestJson.collegeId = {"eventno": "6062","eventdesc": "abc"};
testJson = newTestJson;