简体   繁体   中英

Unable to access JSON Element in Javascript via Object.property

I am unable to access a simple JSON Element from a JSON Structure that looks like:

{
"ACTION": "AA",
"MESSAGE": "Customer: 30xxx Already Approved on 2017/01/01"
}

I get the data in JSON Format but when i do data.ACTION or data.MESSAGE i get Undefined as the output.

By doing case sensitive also, its not working( Image attached ) 在此输入图像描述

var url = base + query;
var getJSON = function (url) {
    return new Promise(function (resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', url, true);
        xhr.withCredentials = true;
        xhr.onload = function () {
            var status = xhr.status;
            if (status == 200) {
                resolve(xhr.response);
            } else {
                reject(status);
            }
        };
        xhr.send();
    });
};

getJSON(url).then(function (data) {
    console.log(data); //Getting JSON Data                   
    var output = JSON.stringify(data);
    var obj = JSON.parse(output.replace(/ 0+(?![\. }])/g, ' '));
    console.log(output);
    console.log(obj.message); //Here getting UNDEFINED                    

}, function (status) { //error detection....
    alert('Something went wrong.');
});

Console:
{"ACTION":"AA","MESSAGE":"Customer No. 0000030332 Already Approved On 20170113"}

stringify returns the following
{\"ACTION\":\"AA\",\"MESSAGE\":\"Customer No. 0000030332 Already Approved On 20170113\"}"

obj.message property is not defined and when you try to get the property which is not defined on an object, you get undefined .

Javascript is case sensitive. You should try obj.MESSAGE instead to get the property value. Also, to check if a property exists on an object you can make use of object.hasOwnProperty([propName]) method to check if a property exists on a object or not.

EDIT 1: Try running the following code snippet. JSON data string is parsed before accessing the property.

 var jsonString = "{\\"ACTION\\":\\"AA\\",\\"MESSAGE\\":\\"Customer No. 0000030332 Already Approved On 20170113\\"}"; var obj = JSON.parse(jsonString); console.log(obj.MESSAGE); 

EDITED. I first thought the error was due to parsing, given the print. -.-

Solution: When you print the output, obj it's still a string, not an object. So it is OK at that point.

Your "undefined" property message should be replaced by MESSAGE . Instead of console.log(obj.message); just use console.log(obj.MESSAGE);

Also. An example of parsing JSON:

var myJson = '{"ACTION":"AA","MESSAGE":"Customer No. 0000030332 Already Approved On 20170113"}';
console.log(myJson);   // This prints the literal string  
console.log(JSON.parse(myJson)); // this prints an "object"

data already is a JSON string, there's no need to JSON.stringify it (which returns a string with a JSON-encoded string literal). Parsing it into output only leads to a string again, which has no properties. You should use

console.log(data);
var obj = JSON.parse(data);
console.log(obj);
obj.MESSAGE = obj.MESSAGE.replace(/ 0+(?![\. }])/g, ' ');

(notice the proper casing of the property name)

You can try:

var jsonObject = data.data;
console.log(jsonObject.ACTION)

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