2012-04-28 111 views
7

转换这样的:如何提取一个JSON对象,JSON对象内的

{"items":[{"id":"BLE89-A0-123-384","weight":"100","quantity":3}, 
      ...  
      {"id":"BLE10-A0-123-321","weight":"100","quantity":4}], 
"country":"JUS", 
"region":"A", 
... 
"timeout":"FILLER"} 

要这样:

{"BLE89-A0-123-384": "3", "BLE10-A0-123-321": "4"}这是... {ID:量}

我找到答案这几乎做我需要的:Searching for an Object inside the JSON。但是这个答案对我没有帮助,因为它只是在第一级(一个json对象)。我的问题是在第二级(json对象内的json对象)。提前致谢!

+1

'{“BLE89-A0-123-384”,“3”; “BLE10-A0-123-321”,“4”}'这甚至不是一个有效的对象,它必须是一个关键值对。 – 2012-04-28 22:41:31

+0

对不起,修好了! – mrmo123 2012-04-28 22:43:45

+0

什么*不*你知道该怎么办?你知道如何访问数组或对象的成员吗?你知道如何迭代一个数组吗?你知道如何创建一个对象,并为其分配新的键/值对吗?很高兴知道你需要什么帮助。 – 2012-04-28 22:56:48

回答

21

它帮助,如果你不认为JSON对象作为JSON对象。一旦通过JSON.parse运行JSON字符串,它就是一个本地JavaScript对象。

在JavaScript中,有两种访问对象的方式。

点表示法

点符号是这样

myObject.name

见点?你可以用它来访问任何对象属性(它确实可能是JavaScript中的另一个对象,只要它具有有效的点符号名称)。您不能使用-.等字符以及空格字符。

括号标记(可能是另一个名字)

myObject["variableName"]

像点符号,但允许其他一些字符,如-和空格..没有完全一样的东西。

使用这些符号很有用,因为我们可以访问嵌套属性。

myObj.foo.bar.baz()

现在让我们到您的JSON对象...

{"items":[{"id":"BLE89-A0-123-384","weight":"100","quantity":3,"stock":0},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4,"stock":0}],

你可能想在JSON format刷了自己,但在你的例子,这里有几个线索..

{意味着一个对象的开始。 (请记住,您的整个JSON字符串本身就是一个对象。)

}意味着对象的结束。

"variable"(带引号!在JSON中很重要,但不能在访问/声明JavaScript对象时使用)将属性分配给对象。

:是JSON和JavaScript对象中的赋值运算符。 :右侧的任何值都是您分配给左侧属性的值。

,表示您正在对象内启动一个新属性。

您可能知道[],之间的逗号意味着一个数组。

当我们通过JSON.parse(string)运行你的字符串,我们会得到一个对象,它看起来像这样...

var myResponse = JSON.parse(response);

现在,您可以使用它作为一个本地JavaScript对象。您要查找的是“项目”内的嵌套属性。

var items = myResponse.items; //alternatively you could just use myResponse.items

由于items数组对象的,我们需要通过它以现有的对象转换成一个新的对象进行迭代。

var i; 
var result = {} ; //declare a new object. 
for (i = 0; i < items.length; i++) { 
    var objectInResponse = items[i]; //get current object 
    var id = objectInResponse.id; //extract the id. 
    var quantity = objectInResponse.quantity; 
    result[id] = quantity; //use bracket notation to assign "BLE89-A0-123-384" 
    //instead of id. Bracket notation allows you to use the value 
    // of a variable for the property name. 

结果现在是一个对象,它看起来像:

{ 
    "BLE89-A0-123-384" : 3, //additional properties designated by comma 
    "BLE10-A0-123-321" : 4 // The last key/value in an object literal CANNOT 
    // have a comma after it! 
} 

您可以访问使用括号标记的属性。

var BLE89 = result["BLE10-A0-123-321"]; //use quotes, otherwise JavaScript will try to look up the value of a variable.

+0

+1进行彻底解释。 – 2012-04-28 23:04:54

+0

哇,这是一个比我预期的问题更彻底的答案:D – epidemian 2012-04-28 23:08:33

+0

感谢您的好评! – mrmo123 2012-04-28 23:10:14

0

你需要做到以下几点:


var newJSON = {}; 
for (var i = 0; i < oldJSON.items.length; i++) { 
    newJSON[oldJSON.items[i].id] = oldJSON.items[i].quantity; 
} 
+0

'.length()'需要是'.length'。长度是属性而不是函数。 – 2012-04-28 23:01:09

2

你可以尝试使用:

var obj = { 
    "items":[ 
     {"id":"BLE89-A0-123-384","weight":"100","quantity":3}, 
     {"id":"BLE10-A0-123-321","weight":"100","quantity":4} 
    ], 
    "country":"JUS", 
    "region":"A", 
    "timeout":"FILLER" 
}; 

var quantities = {}; 
obj.items.forEach(function (item) { 
    quantities[item.id] = item.quantity; 
}); 

quantities然后将对象{"BLE89-A0-123-384":3,"BLE10-A0-123-321":4}forEach是JavaScript中的数组对象的本地方法,可让您遍历其元素。你可能想把这段代码放到一个函数中:

function getQuantities(obj) { 
    var quantities = {}; 
    obj.items.forEach(function (item) { 
     quantities[item.id] = item.quantity; 
    }); 
    return quantities; 
} 
+0

谢谢!!!!!! – mrmo123 2012-04-28 23:15:29