向json对象附加一个键值对

前端之家收集整理的这篇文章主要介绍了向json对象附加一个键值对前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我正在使用的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
        }
    ]
}

我想要这个另一个键值对:

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

我尝试了连接,但这给我的结果与||符号和我cdnt迭代.我使用溢出,但只删除逗号.

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

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

解决方法

这是最简单的方式,它正在为我而工作.
var testJson = {
        "name": "John Smith","address": {
            "street": "701 First Ave.","country": "United States"
        },"children": [
            {
                "name": "Richard","age": 7
            },{
                "name": "Susan","age": 4
            },{
                "name": "James","age": 3
            }
        ]
    };
    testJson.collegeId = {"eventno": "6062","eventdesc": "abc"};
原文链接:https://www.f2er.com/js/154608.html

猜你在找的JavaScript相关文章