如何提取 json 对象内的 json 对象

How to extract a json object that's inside a json object

本文关键字:json 对象 提取 何提取      更新时间:2023-09-26

转换这个:

{"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: 数量}

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

如果您不将 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 格式,但在您的示例中,这里有一些线索......

{ 表示对象的开始。 (请记住,整个 JSON 字符串本身就是一个对象。

} 表示对象的结束。

"variable"(在 JSON 中带有引号!很重要,但在访问/声明 javascript 对象时不重要(为您的对象分配一个属性。

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

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

您可能知道内部带有逗号, []意味着一个数组。

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

var myResponse = JSON.parse(response);

您现在可以将其用作本机 JavaScript 对象。 您要查找的是"items"中的嵌套属性。

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.

你可以试试:

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;
}

您需要执行以下操作:


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