[英]Parse data from JSON response
I have a JSON response data as follows 我有一个JSON响应数据,如下所示
{"response":"9",
"status":"Success",
"msg":"Valid Access",
"data":[{"id":"1","title":"A"},
{"id":"2","title":"B"},
{"id":"3","title":"C"}]
}
How can I fetch only the data array using jquery? 如何使用jquery只获取数据数组?
Showing error as parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data
将错误显示为
parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data
parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data
in console while parse/stringify parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data
解析/字符串化时,控制台中parsererror; SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 666 of the JSON data
Use dot notation 使用点符号
var obj = {"response":"9",
"status":"Success",
"msg":"Valid Access",
"data":[{"id":"1","title":"A"},
{"id":"2","title":"B"},
{"id":"3","title":"C"}]
}
console.log(obj.data);
https://jsfiddle.net/c8z35au4/ https://jsfiddle.net/c8z35au4/
or parse the data 或解析数据
var unparsed = '{"response":"9",
"status":"Success",
"msg":"Valid Access",
"data":[{"id":"1","title":"A"},
{"id":"2","title":"B"},
{"id":"3","title":"C"}]
}';
var obj = JSON.stringify(unparsed);
console.log(obj.data);
If it's Ajax request: 如果是Ajax请求:
$.ajax({
dataType: 'json',
success: function (response) {
console.log(response.data);
}
});
If it's string, than use var response = JSON.parse(string)
and response.data
如果是字符串,则使用
var response = JSON.parse(string)
和response.data
var data = {"response":"9", "status":"Success", "msg":"Valid Access", "data":[{"id":"1","title":"A"}, {"id":"2","title":"B"}, {"id":"3","title":"C"}] } console.log(JSON.stringify(data.data))
The best way is 最好的方法是
var data = {"response":"9",
"status":"Success",
"msg":"Valid Access",
"data":[{"id":"1","title":"A"},
{"id":"2","title":"B"},
{"id":"3","title":"C"}]
}
data=JSON.parse(data);
var requiredData=data.data;
for(var i in requiredData)
{
var id = requiredData[i].id;
var title = requiredData[i].title;
console.log(id);
console.log(title);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.