用JS解析JSON嵌套数组

时间:2020-01-28 12:23:53

标签: javascript json

我在这里使用相似的JSON遍历了各种主题,但是从来都不一样,并且我仍然在循环遍历数组的元素方面很挣扎。

我有以下JSON:

#if _WINDLL
        sprintf_s(DestinationArray, SourceString.c_str()); //Syntax using sprintf_s for Windows
#else

        snprintf(DestinationArray, SourceString.length(), SourceString.c_str()); //syntax using snprintf For Linux

#endif

JSON预先作为字符串传递。 然后,我尝试列出所有“人员”及其ID和属性列表:

     {
     "person": 
     [
        {
            "id": "xyz",
            "attributes": 
            [
            {"attribute_name1": "value1"},
            {"attribute_name2": "value2"}
            ]
        },
        {
            "id": "abc1",
            "attributes": 
            [
            {"attribute_name1": "value3"},
            {"attribute_name2": "value4"},
{"attribute_name3": "value77"}
            ]
        }
    ]
}

如果有任何帮助,我很高兴更改JSON的结构。如果属性名称不是“固定的”,那么如何获取这些属性名称的值的任何提示?

5 个答案:

答案 0 :(得分:0)

也许这会有所帮助。更改JSON结构

var person = [{id:1, name: 'John'},{id:2, name: 'Mary'}];
var attributes = [
 {person_id:1, name: 'attr_name', value: 'val1'},
  {person_id:2, name: 'attr_name2', value: 'val2'}
  ];
 
 // variant 1
 for (let attr of attributes) {
   if (attr.person_id === targetPersonId) { ... }
 }
 
 // variant 2
attributes.map(el => el.person_id === targetPersonId).forEach(... do something)

答案 1 :(得分:0)

您可以将此代码段与现有的JSON结构一起使用。

const data = '{"person":[ { "id": "xyz","attributes": [{"attribute_name1": "value1"},{"attribute_name2": "value2"}]},{"id": "abc1","attributes": [{"attribute_name1": "value3"},{"attribute_name2": "value4"},{"attribute_name3": "value77"}]}]}';

const json = JSON.parse(data);

json.person.forEach((person) => {
  person.attributes.forEach((attribute) => {
    const properties = Object.keys(attribute);
    console.log(`Person's ID:`, person.id);
    properties.forEach((prop) => {
      console.log(`${prop} ${attribute[prop]}`);
    });
  });
});

答案 2 :(得分:0)

json.person[i].attributes[j]['attribute_name'](即json.person[i].attributes[j][attribute_name])中删除单引号。

var data = '{"person":[ { "id": "xyz","attributes": [{"attribute_name1": "value1"},{"attribute_name2": "value2"}]},{"id": "abc1","attributes": [{"attribute_name1": "value3"},{"attribute_name2": "value4"},{"attribute_name3": "value77"}]}]}';
var json, i, j;

json = JSON.parse(data);
for (i in json.person) {
  for (j in json.person[i].attributes) {
    var attribute_name = Object.keys(json.person[i].attributes[j]);
    console.log("Person's ID: " +json.person[i].id + " /// Person's Attribute name: " + attribute_name + " /// Person's Value: " +json.person[i].attributes[j][attribute_name]);
    console.log("Person's Value: " +json.person[i].attributes[j][attribute_name]);
  }
}

答案 3 :(得分:0)

如果想要从json获取名称/值的对象,则可以尝试使用类似的方法。

var object = {
     "person": 
     [
        {
            "id": "xyz",
            "attributes": 
            [
            {"attribute_name1": "value1"},
            {"attribute_name2": "value2"}
            ]
        },
        {
            "id": "abc1",
            "attributes": 
            [
            {"attribute_name1": "value3"},
            {"attribute_name2": "value4"},
            {"attribute_name3": "value77"}
            ]
        }
    ]
}



var someobj = {};

function generate(obj) {
    Object.keys(obj).forEach(key => {
        if (key !== 'key' && key !== 'value') {
            obj[key].forEach(element => {
                someobj[element.id] = element.attributes;
                return someobj;
            });
        }
    })
}

答案 4 :(得分:0)

<script>
    var data = {
        person: [{
            id: "xyz", attributes: [{ attribute_name1: "value1" },
            { attribute_name2: "value2" }]
        },
        {
            id: "abc1", attributes: [{ attribute_name1: "value3" },
            { attribute_name2: "value4" }, { attribute_name3: "value77" }]
        }]
    };

    var json, i, j;
    //var json = data.toString();

    //json = JSON.parse(json);

    for (i in data.person) {
        console.log("Person's ID: " + data.person[i].id);
        for (j in data.person[i].attributes) {
            var attribute_name = Object.keys(data.person[i].attributes[j])[0]; // here because I won't know what are the actual names of those attributes
            console.log("Person's Value: " +attribute_name +"->" + data.person[i].attributes[j][attribute_name]); // this gives undefined
        }
    }
</script>