简体   繁体   中英

unable to retrieve object from json file using key as control id

I have a json file with control id as key and value under response body as mentioned:

"Header": {
      "Data": {
         "@idType": "lab",
         "@id": "271093",
         "@name": "Rahul",
         "@techspecs": ""
      },
      "Template": {
         "@variant": "NA"
      },
      "Document": {
         "@name": "NA",
         "@exported": "NA"
      }
   },
   "Response": [
      {
         "@ctrl": "95245E0B-6D52-4398-8749-0090C8FC5CB9",
         "ResponseString": "+919844300558"
      }


I have written a code to retrieve a single response as there are lot of response. 
 const val1 = Object.keys(95245E0B-6D52-4398-8749-0090C8FC5CB9).number

but its not working

You can use find() to get a response with a specific key:

 const data = { "Header": { "Data": { "@idType": "lab", "@id": "271093", "@name": "Rahul", "@techspecs": "" }, "Template": { "@variant": "NA" }, "Document": { "@name": "NA", "@exported": "NA" } }, "Response": [{ "@ctrl": "95245E0B-6D52-4398-8749-0090C8FC5CB9", "ResponseString": "+919844300558" }] }; const response = data.Response.find(response => response['@ctrl'] === '95245E0B-6D52-4398-8749-0090C8FC5CB9').ResponseString; console.log(response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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