我正试图从我的JSON获取ID,但似乎我做了一些愚蠢的事情。
请查看下面的json和jquery代码。
myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMPI","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';
var myData = JSON.parse(myData);
$(document).ready(function () {
$.each(myData, function () {
$('<li>' + this.result.ID + '</li>').appendTo('#groups');
});
});
如果有人能帮我解决我的问题,我会非常感激。请随时修改我的jsfiddle:
答案 0 :(得分:2)
您的JSON
格式不正确,当您尝试解析时,您会获得SyntaxError: Unexpected token r
。
var data= 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';
data=data.replace('result(','');
data=data.replace(');','');
var parsedData= JSON.parse(data);
// you can now get ID ( example : parsedData.result[0].ID )
答案 1 :(得分:1)
尝试
// call `JSON.stringify` with `myData` argument
var myData = JSON.stringify({
"respond": 1,
"paging": {
"stillmore": 0,
"perpage": 10,
"callpage": 1,
"next": 2,
"previous": 0,
"pages": 1,
"result": "1"
},
"message": "",
"result": [{
"ID": "1",
"user_registered": "13th February 2015",
"display_name": "SMIO API",
"description": "",
"custom_fields": {
"job_title": "Developer",
"company_name": "SMIO",
"telephone_number": "+1 343 5325 6456",
"thumbnail_id": "domain.com\/wp-content\/uploads\/ava.jpg"
}
}]
});
// call `JSON.parse` with `myData` argument
var res = JSON.parse(myData);
$(document).ready(function () {
// call `$.each` with `res.result` array argument
$.each(res.result, function (key, val) {
console.log(key, val);
$("<li>" + val.ID + "</li>").appendTo("#groups");
});
});
jsfiddle http://jsfiddle.net/pae5f3j7/3/
替代地
function result(data) {
return data
};
myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]});';
myData = eval(myData);
$(document).ready(function () {
$.each(myData.result, function (key, val) {
console.log(key, val);
$("<li>" + val.ID + "</li>").appendTo("#groups");
});
});
jsfiddle http://jsfiddle.net/pae5f3j7/5/
// removed semicolon `;` at close of string
myData = 'result({"respond":1,"paging":{"stillmore":0,"perpage":10,"callpage":1,"next":2,"previous":0,"pages":1,"result":"1"},"message":"","result":[{"ID":"1","user_registered":"13th February 2015","display_name":"SMIO API","description":"","custom_fields":{"job_title":"Developer","company_name":"SMIO","telephone_number":"+1 343 5325 6456","thumbnail_id":"domain.com\/wp-content\/uploads\/ava.jpg"}}]})';
myData = JSON.parse(myData.split(/^.*\(|\)$/)[1]);
$(document).ready(function () {
$.each(myData.result, function (key, val) {
console.log(key, val);
$('<li>' + val.ID + '</li>').appendTo('#groups');
});
});
jsfiddle http://jsfiddle.net/pae5f3j7/6/
答案 2 :(得分:1)
jQuery $ .each采用带有两个变量的匿名函数,通常称为idx和elem,引用数组的索引和数组的元素。
你的。每个函数应该看起来更像这样:
$(myData).each(function (idx, elem) {
var item = "<li>" + $(elem).result.ID + "</li>";
$("#groups").append(item);
});
这假设myData是一个Jquery数组。