简体   繁体   English

如何使用javascript访问嵌套的JSON对象值

[英]how to access nested JSON object values using javascript

I am facing issue in converting json response in a required format. 我在以所需格式转换json响应时遇到问题。 Request: 请求:

{
   "firstName": "ABC",
   "middleName": "V",
   "AddrCity": "CITY",
   "AddressLine2": "ADDRESS LINE 2",
   "LastName": "LASTNAME",
   "AddressLine1": "ADDR LINE1",
   "Country": "India",
   "customerId": "1234",
   "AddrPinCode": "999999"
}

Below is the response i am getting Response: 以下是我得到响应的响应:

{"return": 
 {
   "response": [{"$": 1234}],
   "responseMessage": [{"$": "Success ABC"}],
   "responseCode": [{"$": "CITY,India"}]
 }
}

Notice the "$" symbol, which is giving problem while fetching the response. 注意“ $”符号,在获取响应时出现问题。 Below is the expected response and also need to fetch response, responseMessage & responseCode values accordingly 以下是预期的响应,还需要相应地获取响应,responseMessage和responseCode值

{"return": 
 {
   "response": 1234,
   "responseMessage": "Success ABC",
   "responseCode": "CITY,India"
 }
}

Thanks for your quick response in advance. 感谢您提前的快速回复。

var obj={"return": 
 {
   "response": [{"$": 1234}],
   "responseMessage": [{"$": "Success ABC"}],
   "responseCode": [{"$": "CITY,India"}]
 }
};

obj.return.response[0].['$'];
obj.return.responseMessage[0].['$'];
obj.return.responseCode[0].['$'];

Try out this solution: 试试这个解决方案:

You can try this two ways 您可以尝试这两种方式

Way 1:(preferred) 方式1 :(首选)

  let str = `{"return": { "response": [{"$": 1234}], "responseMessage": [{"$": "Success ABC"}], "responseCode": [{"$": "CITY,India"}] } }`; let js_object = JSON.parse(str); // parse json string to javascript object let js_object_return = js_object.return; let formated_obj = {}; let desired_obj = {}; Object.keys(js_object_return).forEach(function(key) { formated_obj[key] = js_object_return[key][0]["$"]; }); desired_obj['return']=formated_obj; console.log(desired_obj.return.response); 

Way 2: 方式2:

 let regex = /\\[{"\\$": ("?([a-z0-9A-Z\\s,]+"?))}\\]/gm; let str = `{"return": { "response": [{"\\$": 1234}], "responseMessage": [{"\\$": "Success ABC"}], "responseCode": [{"\\$": "CITY,India"}] } }`; let subst = `$1`; // The substituted value will be contained in the result variable let result = str.replace(regex, subst); let desired_object = JSON.parse(result); // parse json string to javascript object console.log(desired_object.return.response); 

You can access the value inside the JSON using bracket notation . 您可以使用方bracket notation访问JSON中的值。

 var str = '{"return": {"response": [{"$": 1234}],"responseMessage": [{"$": "Success ABC"}],"responseCode": [{"$": "CITY,India"}]}}'; var obj = JSON.parse(str); console.log(obj.return.response[0]['$']); console.log(obj.return.responseMessage[0]['$']); console.log(obj.return.responseCode[0]['$']); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You can use array#foreach and Object.key() to get the desired object. 您可以使用array#foreachObject.key()获得所需的对象。

 var str = '{"return": {"response": [{"$": 1234}],"responseMessage": [{"$": "Success ABC"}],"responseCode": [{"$": "CITY,India"}]}}'; var obj = JSON.parse(str); Object.keys(obj.return).forEach((key) => { obj.return[key] = obj.return[key][0]['$']; }); console.log(obj) 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

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