[英]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: 我无法从JSON结构访问一个简单的JSON元素,如下所示:
{
"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. 我以JSON格式获取数据,但是当我执行data.ACTION或data.MESSAGE时,我将Undefined作为输出。
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
. obj.message
属性,当您尝试获取未在对象上定义的属性时,您将获得undefined
。
Javascript is case sensitive. Javascript区分大小写。 You should try obj.MESSAGE
instead to get the property value. 您应该尝试使用obj.MESSAGE
来获取属性值。 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. 此外,要检查对象上是否存在属性,可以使用object.hasOwnProperty([propName])
方法检查对象上是否存在属性。
EDIT 1: Try running the following code snippet. 编辑1:尝试运行以下代码段。 JSON data string is parsed before accessing the property. 在访问属性之前解析JSON数据字符串。
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. 解决方案:打印输出时, obj
仍然是字符串,而不是对象。 So it is OK at that point. 所以那时候还可以。
Your "undefined" property message
should be replaced by MESSAGE
. 您的“未定义”属性message
应由MESSAGE
替换。 Instead of console.log(obj.message);
而不是console.log(obj.message);
just use console.log(obj.MESSAGE);
只需使用console.log(obj.MESSAGE);
Also. 也。 An example of parsing JSON: 解析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). data
已经是一个JSON字符串,不需要JSON.stringify
它(它返回一个带有JSON编码的字符串文字的字符串)。 Parsing it into output
only leads to a string again, which has no properties. 将其解析为output
只会再次导致字符串,该字符串没有属性。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.