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

时间:2017-08-28 16:20:29

标签: javascript json

我在以所需格式转换json响应时遇到问题。 请求:

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

以下是我得到的回复 回复:

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

注意" $"符号,在获取响应时给出问题。 下面是预期的响应,还需要获取响应,responseMessage&相应的responseCode值

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

感谢您提前快速回复。

3 个答案:

答案 0 :(得分:0)

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

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

尝试这个解决方案:

答案 1 :(得分:0)

您可以尝试这两种方式

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




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




答案 2 :(得分:0)

您可以使用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; }

您可以使用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; }