Javascript 如何提取json对象内的json对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10368171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract a json object that's inside a json object
提问by mrmo123
Convert this:
转换这个:
{"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"}
To this:
对此:
{"BLE89-A0-123-384": "3", "BLE10-A0-123-321": "4"}
that is... {id: quantity}
{"BLE89-A0-123-384": "3", "BLE10-A0-123-321": "4"}
那就是... {id: 数量}
I found an answer that almost does what I need: Searching for an Object inside the JSON. But that answer doesn't help me because it's only at the first level (one json object). My problem is at the second level (json object within a json object). Thanks in advance!
我找到了一个几乎可以满足我需要的答案:Searching for an Object inside the JSON。但是这个答案对我没有帮助,因为它只是在第一级(一个 json 对象)。我的问题是在第二级(json 对象中的 json 对象)。提前致谢!
回答by Stanley Stuart
It helps if you don't think of JSON objects as JSON objects. Once you run a JSON string through JSON.parse, it is a native JavaScript object.
如果您不将 JSON 对象视为 JSON 对象,它会有所帮助。一旦你通过 JSON.parse 运行一个 JSON 字符串,它就是一个原生的 JavaScript 对象。
In JavaScript, there are two ways to access objects.
在 JavaScript 中,有两种访问对象的方法。
Dot Notation
点符号
The dot notation goes like this
点符号是这样的
myObject.name
myObject.name
See the dot? You can use that to access any object property(which indeed may be another object in javascript, as long as it has a valid dot notation name). You can't use characters like -
, .
, and the space character.
看到点了吗?您可以使用它来访问任何对象属性(它确实可能是 javascript 中的另一个对象,只要它具有有效的点符号名称)。你不能使用像字符-
,.
和空格字符。
Bracket Notation (may be another name)
括号符号(可能是另一个名称)
myObject["variableName"]
myObject["variableName"]
Like dot notation but allows some other characters, like -
and the space character.. Does exactly the same thing.
像点符号但允许一些其他字符,像-
和空格字符.. 做完全一样的事情。
Using these notations is useful because we can access nested properties.
使用这些符号很有用,因为我们可以访问嵌套属性。
myObj.foo.bar.baz()
myObj.foo.bar.baz()
Now let's get to your JSON object...
现在让我们来看看你的 JSON 对象...
{"items":[{"id":"BLE89-A0-123-384","weight":"100","quantity":3,"stock":0},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4,"stock":0}],
{"items":[{"id":"BLE89-A0-123-384","weight":"100","quantity":3,"stock":0},
{"id":"BLE10-A0-123-321","weight":"100","quantity":4,"stock":0}],
You might want to brush up on the JSON formatyourself, but in your example, here's a few clues...
您可能想自己复习 JSON 格式,但在您的示例中,这里有一些线索......
{
Means the start of an object. (Keep in mind your entire JSON string is an object itself.)
{
表示对象的开始。(请记住,您的整个 JSON 字符串本身就是一个对象。)
}
Means the end of an object.
}
表示对象的结束。
"variable"
(with quotes! important in JSON, but not when accessing/declaring javascript objects) assigns a property to your object.
"variable"
(带引号!在 JSON 中很重要,但在访问/声明 javascript 对象时不重要)为您的对象分配一个属性。
:
Is the assignment operator in both JSON and JavaScript objects.
Anything to the right of the :
is the value you are assigning to the property on the left.
:
是 JSON 和 JavaScript 对象中的赋值运算符。右侧的任何内容:
都是您分配给左侧属性的值。
,
Means you are starting a new property within an object.
,
意味着你正在一个对象中开始一个新的属性。
You probably know that []
with ,
commas inside means an array.
你可能知道,[]
与,
内部手段逗号的数组。
When we run your string through JSON.parse(string)
, we'll get an object that looks like this...
当我们运行你的字符串时JSON.parse(string)
,我们会得到一个看起来像这样的对象......
var myResponse = JSON.parse(response);
var myResponse = JSON.parse(response);
You can now use it as a native JavaScript object. What you're looking for is a nested property within "items".
您现在可以将其用作本机 JavaScript 对象。您正在寻找的是“项目”中的嵌套属性。
var items = myResponse.items; //alternatively you could just use myResponse.items
var items = myResponse.items; //alternatively you could just use myResponse.items
Since items
is an array of objects, we'll need to iterate through it in order to convert the existing object into a new object.
由于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.
Result is now an object that looks like:
结果现在是一个如下所示的对象:
{
"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!
}
You can access the properties using bracket notation.
您可以使用括号表示法访问属性。
var BLE89 = result["BLE10-A0-123-321"]; //use quotes, otherwise JavaScript will try to look up the value of a variable.
var BLE89 = result["BLE10-A0-123-321"]; //use quotes, otherwise JavaScript will try to look up the value of a variable.
回答by epidemian
You can try with:
您可以尝试:
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
will then be the object {"BLE89-A0-123-384":3,"BLE10-A0-123-321":4}
. forEach
is a native method of array objects in JavaScript that lets you iterate through their elements. You may want to put that piece of code inside a function:
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;
}
回答by ControlAltDel
You need to do the following:
您需要执行以下操作:
var newJSON = {};
for (var i = 0; i < oldJSON.items.length; i++) {
newJSON[oldJSON.items[i].id] = oldJSON.items[i].quantity;
}