在React Native中进行JSON解析以获取字符串数组

时间:2019-03-28 18:14:55

标签: javascript reactjs react-native jsx

在以下JSON的本机React中,我需要获取array的{​​{1}} [“ abc description”,“ def description”,“ ghi description”等]值stringDataHolder中可能有n个dictionary

DataHolder

我是本机新手,将不胜感激。

2 个答案:

答案 0 :(得分:2)

听起来像您想从API响应中重组数据。这是一种解决方案:

const data = {
    ...
    "Response": {
        "Data": {
            "DataHolder": [
                {
                    "abc": "abc description"
                },
                {
                    "def": "def description"
                },
                {
                    "ghi": "ghi description"
                }
            ]
        }
    }
};
const descriptions = data.Response.Data.DataHolder.map(item => Object.values(item)[0]);

// ["abc description", "def description", "ghi description"]

答案 1 :(得分:0)

一种解决方案是使用JSON库

 const tempJSON = {
      "StatusCode": 200,
      "Description": "Description",
      "Message": "Success",
      "Response": {
        "Data": {
          "DataHolder": [
            {
              "abc": "abc description"
            },
            {
              "def": "def description"
            },
            {
              "ghi": "ghi description"
            }
          ]
        }
      }
    }

    let resultJSON = JSON.stringify(tempJSON)
    resultJSON = JSON.parse(resultJSON)

    console.log(resultJSON.Response.Data.DataHolder)

记录结果

(3) [{…}, {…}, {…}]
0: {abc: "abc description"}
1: {def: "def description"}
2: {ghi: "ghi description"}