javascript - 用Js操作json

标签 javascript json data-manipulation

我有一个如下所示的 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/

相关文章:

javascript - 如何使用 anchor 标记与 $stateProvider 重定向到 Angular js 中的另一个页面?

javascript - 排序并查找距当前时间最近的时间

删除字符串 R 数据帧之后出现的行

r - 用于选择一个或多个变量范围内的观测值的函数 (dplyr)

r - 具有不同分母 R 的除法的累积和

javascript - 使 slider 不超过视口(viewport)的高度

javascript - 将 sin() 近似为 {1, 0, -1} 会给出错误的结果

javascript - 无法使用 PHP 读取 ajax 请求

javascript - JSON 对象 .. JSON 对象内部

python - 将 CSV 转换为 JSON,同时删除某些列