[英]How to access json object using javascript?
I am not an expert in json parsing so please bear with me if i ask simple question.I have a json response like this : 我不是json解析的专家,所以如果我问一个简单的问题,请耐心等待。我有这样的json响应:
{
"resp": [{
"Key": "123423544235343211421412",
"id": "12"
}]
}
I want to access value of key and id (123423544235343211421412,12) .I tried following but i can't get the values! 我想访问键和ID的值(123423544235343211421412,12)。我尝试了以下操作,但无法获取这些值!
I appreciate if you guys show me how to get those values.Thanks 如果您向我展示了如何获得这些价值,我将不胜感激。
var postData = {
Name: "Galaxy",
action: "token"
};
$.ajax("https://someapiurl/getit.aspx",{
type : 'POST',
data: JSON.stringify(postData),
contentType: "application/json",
success: function(data) {
var json = JSON.parse(data);
alert(json.resp[0].Key);
alert(json.resp[1].id);
},
contentType: "application/json",
dataType: 'json'
});
You're almost there. 你快到了。 jQuery automatically parses the response as JSON for you when you specify dataType: 'json'
当您指定dataType: 'json'
时,jQuery会自动为您将响应解析为JSON。
$.ajax('https://someapiurl/getit.aspx', {
method: 'POST',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
data: JSON.stringify(postData)
}).done(function(obj) {
alert(obj.resp[0].Key);
alert(obj.resp[0].id);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.