简体   繁体   English

解析json中的动态参数

[英]Parse dynamic parameter in json

Hi I am new for javascript, I googling few hour but still not able to get any answer.嗨,我是 javascript 新手,我在谷歌上搜索了几个小时,但仍然无法得到任何答案。 Here is my question.这是我的问题。 I calling api and getting with below result我调用 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"
   }
}

However How i can parse those "2017.10, 2017.7 , 2017.4 " and etc to my JSON object ?但是,我如何将那些“2017.10、2017.7、2017.4”等解析为我的JSON 对象 Thanks谢谢

That is valid JSON.这是有效的 JSON。 You can access the values of data using bracket notation:您可以使用括号表示法访问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'];

You can map over the Object.keys of data :您可以map dataObject.keys

 const response = { "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" } } const data = response.data; const dates = Object.keys(data).map(key => key); console.log(dates);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM