gpt4 book ai didi

json - 将键值对附加到 json 对象

转载 作者:行者123 更新时间:2023-12-04 01:51:55 24 4
gpt4 key购买 nike

这是 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"
};

我试过 concat 但结果是 ||符号,我 cdnt 迭代。我使用了溢出但只删除了逗号。
concattedjson = JSON.stringify(JSON.parse(json1).concat(JSON.parse(json2)));

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

最佳答案

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

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"};

关于json - 将键值对附加到 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com