如何提取json对象内的json对象 [英] How to extract a json object that's inside a json object

查看:288
本文介绍了如何提取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:quantity}

{"BLE89-A0-123-384": "3", "BLE10-A0-123-321": "4"} that is... {id: quantity}

我找到的答案几乎可以满足我的需求:在JSON中搜索对象。但是这个答案对我没有帮助,因为它只在第一级(一个json对象)。我的问题是在第二级(json对象中的json对象)。在此先感谢!

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!

推荐答案

如果您不将JSON对象视为JSON对象,这会有所帮助。通过JSON.parse运行JSON字符串后,它就是一个本机JavaScript对象。

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.

在JavaScript中,有两种方法可以访问对象。

In JavaScript, there are two ways to access objects.

点符号如下所示

myObject.name

看点?您可以使用它来访问任何对象 property (实际上它可能是javascript中的另一个对象,只要它具有有效的点符号名称)。您不能使用 - 等字符以及空格字符。

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.

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()

现在让我们来看看你的JSON对象......

Now let's get to your JSON object...

{items:[{id:BLE89-A0-123-384,weight:100,quantity:3,stock:0 },

{id:BLE10-A0-123-321,重量:100,数量:4,股票:0}],

您可能想要了解 JSON格式你自己,但在你的例子中,这里有一些线索......

You might want to brush up on the JSON format yourself, 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.)

} 表示对象的结束。

variable(带引号!在JSON中很重要,但在访问/声明javascript对象时却没有)指定一个对象的属性。

"variable" (with quotes! important in JSON, but not when accessing/declaring javascript objects) assigns a property to your object.

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.

表示您在对象中启动新属性。

, Means you are starting a new property within an object.

您可能知道 [] 逗号里面表示数组。

You probably know that [] with , commas inside means an array.

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

When we run your string through JSON.parse(string), we'll get an object that looks like this...

var myResponse = JSON.parse(response);

现在可以将它用作本机JavaScript对象。你要找的是items中的嵌套属性。

You can now use it as a native JavaScript object. What you're looking for is a nested property within "items".

var items = myResponse.items; //或者你可以使用myResponse.items

因为 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.

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]; //使用引号,否则JavaScript会尝试查找变量的值。

这篇关于如何提取json对象内的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆