我有一个如下所示的 json。您可以看到 same here 的 TreeView .
{
"results": {
"collection1": [
{
"property1": {
"href": "http://www.somesite.com/neatobambino/2015/12/29/Little-Girl-Uses-Dogs-Tail-as-a-Paintbrush/",
"text": "Little Girl Uses Dog's Tail as a Paintbrush"
},
"property5": {
"alt": "",
"src": "",
"text": ""
},
"index": 1,
"url": "http://www.somesite.com/"
},
{
"property1": {
"href": "http://www.somesite.com/2015/09/11/20-Alternative-Housing-Solutions-for-the-Homeless/",
"text": "20 Alternative Housing Solutions for the Homeless"
},
"property5": [
{
"alt": "",
"src": "http://www.somesite.com/page/data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=",
"text": ""
},
{
"alt": "",
"src": "http://www.somesite.com/page/data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=",
"text": ""
}
],
"index": 2500,
"url": "http://www.somesite.com/page/84"
}
]
}
}
如您所见,结果内部有两层。在 results
中,我们再次拥有 collection1
。整个 json 都在对象 data
中。如何将 collection1
中的全部数据直接移动到 results
?
类似的东西。
var data.results = data.results.collections;
你能做到吗?
最佳答案
是的,应该可以,检查这个 fiddle
obj.results = obj.results.collection1;
console.log( obj );
关于javascript - 用Js操作json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528687/