简体   繁体   English

无法使用密钥作为控制 ID 从 json 文件中检索 object

[英]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:我有一个 json 文件,其中控制 id 作为响应正文下的键和值,如上所述:

"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:您可以使用find()获得具有特定键的响应:

 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);

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

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