在json中解析动态参数

时间:2017-09-28 21:36:20

标签: json reactjs parsing

您好我是javascript的新手,我谷歌搜索几个小时但仍然无法得到任何答案。这是我的问题。我打电话给api并得到以下结果

 {  
   "return_status":1,
   "return_message":"success",
   "data":{  
      "2017.10":[  
         {  
            "tree_id":"BSRCC001",
            "tree_code":"1240",
            "tree_report_code":"5",
            "scan_date":"11.10.2017, 09:57AM",
            "scan_year":"2017",
            "scan_month":"10"
         }
      ],
      "2017.7":[  
         {  
            "tree_id":"BSRCC001",
            "tree_code":"1240",
            "tree_report_code":"4",
            "scan_date":"31.07.2017, 09:22AM",
            "scan_year":"2017",
            "scan_month":"7"
         }
      ],
      "2017.4":[  
         {  
            "tree_id":"BSRCC001",
            "tree_code":"1240",
            "tree_report_code":"3",
            "scan_date":"13.04.2017, 09:21AM",
            "scan_year":"2017",
            "scan_month":"4"
         }
      ],

      "total_scan":"6"
   }
}

但是我如何解析那些“2017.10,2017.7,2017.4”等等到我的 JSON对象?感谢

2 个答案:

答案 0 :(得分:1)

这是有效的JSON。您可以使用括号表示法访问data的值:

var obj = JSON.parse(response.body);
var a = obj.data['2017.10'];
var b = obj.data['2017.7'];
var c = obj.data['2017.4'];

答案 1 :(得分:1)

您可map超过Object.keys的{​​{1}}:

data