私はJSONを初めて使いこなしており、本当にこれに苦労しています。私は無数の他の投稿やウェブページを読んだことがあるが、それを理解できないようだ。入れ子になったJSONをJQueryで解析する
私はこのコードを(データベースからのデータから)出力JSONにPHPを使用しています:ここで
header('Content-type: application/json');
echo json_encode($data);
はJSONです:
{
"x0": {
"id": "1",
"name": "Rob",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
},
"x1": {
"id": "2",
"name": "Dave",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
}
}
私は思う私がいる問題JSONがネストされているということです(そこに間違っている可能性があります)。
これはjQueryのです:
function fetchProfiles() {
var url='http://url.com/here';
var i = 0;
var handle = 'x'.i;
$.getJSON(url,function(json){
$.each(json.results,function(i,profile){
$("#profiles").append('<p><img src="'+profile.handle.image+'" widt="48" height="48" />'+profile.handle.name+'</p>');
i++;
});
});
}
感謝任意のアイデアや提案!
ありがとうございます!
あなたのJSONは、まさにこのようになりますか?私は配列を期待するだろう...そしてそれは本当に "結果"と呼ばれるルートキーを持っていますか? – polarblau
JSONファイルの最上位レベルはオブジェクトまたは配列です。 – Quentin
はい、それはPHPのJSON出力です。 .resultsに関する良い点 - そこにコピー&ペーストエラーがあります。 –