[英]How to retrieve JSON values to Javascript
I'm trying to retrieve the JSON data given below but I'm unable to. 我正在尝试检索下面给出的JSON数据,但无法。
Since I'm using Javascript Ajax success function, when I try doing alerts with the code, 由于我使用的是Javascript Ajax成功功能,因此当我尝试使用代码进行提醒时,
$.ajax({
type:'GET',
url:myURL,
success : function(data) {
alert(data);
//{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}
}
});
I am retrieving the below JSON data. 我正在检索以下JSON数据。
{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}
But when I try trying to get the value of mainIsActive using: 但是当我尝试使用以下方法获取mainIsActive的值时:
alert(data.object1.mainIsActive);
I am getting the error in the console: 我在控制台中收到错误:
"Cannot read property 'mainIsActive' of undefined at Object.success (:143:30)" “无法读取Object.success上未定义的属性'mainIsActive'(:143:30)”
Can you please help? 你能帮忙吗? I also attached the JSON image so you can understand the structure better.
我还附加了JSON图片,以便您可以更好地了解其结构。
使用语法解析后,JSON数据将在对象结构中可用
JSON.parse(StringYouWantToParse)
This code seems to work properly: 此代码似乎正常工作:
var x = '{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}';
var data = JSON.parse(x);
alert(data.object1.mainIsActive);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.