我在php中的var里面有这个json内容:
{
"data": [
{
"id": "1977103383661",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "MUM Invasion Photos",
"link": "http://www.facebook.com/album.php?aid=2117387&id=1125405534",
"cover_photo": "1977103423662",
"privacy": "friends",
"count": 1,
"type": "normal",
"created_time": "2011-05-06T12:21:30+0000",
"updated_time": "2011-05-06T12:21:30+0000"
},
{
"id": "1954051927389",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Milan",
"location": "Milan",
"link": "http://www.facebook.com/album.php?aid=2115307&id=1125405534",
"cover_photo": "1954052447402",
"privacy": "friends",
"count": 26,
"type": "normal",
"created_time": "2011-04-25T11:58:14+0000",
"updated_time": "2011-05-07T17:18:10+0000"
},
{
"id": "1875623126718",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Wall Photos",
"link": "http://www.facebook.com/album.php?aid=2108888&id=1125405534",
"cover_photo": "1875623166719",
"privacy": "everyone",
"count": 1,
"type": "wall",
"created_time": "2011-03-28T02:43:54+0000",
"updated_time": "2011-03-28T02:43:54+0000"
},
{
"id": "1832680213172",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "PhotoAge for iPhone\u00ae",
"description": "\"PhotoAge for iPhone\u00ae\" por Percipo Inc. Haciendo computadoras mas humano...",
"link": "http://www.facebook.com/album.php?aid=2106226&id=1125405534",
"cover_photo": "1832680293174",
"privacy": "friends",
"count": 3,
"type": "normal",
"created_time": "2011-03-05T18:18:09+0000",
"updated_time": "2011-03-05T18:20:52+0000"
},
{
"id": "1718288673455",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Camera+ Photos",
"link": "http://www.facebook.com/album.php?aid=2097024&id=1125405534",
"cover_photo": "1718288753457",
"privacy": "friends",
"count": 2,
"type": "normal",
"created_time": "2010-12-26T22:38:52+0000",
"updated_time": "2010-12-26T22:39:14+0000"
},
{
"id": "1716820796759",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Galaxy Tab Contest",
"link": "http://www.facebook.com/album.php?aid=2096857&id=1125405534",
"cover_photo": "1716827796934",
"privacy": "custom",
"count": 5,
"type": "normal",
"created_time": "2010-12-26T00:50:27+0000",
"updated_time": "2011-01-31T19:46:19+0000"
},
{
"id": "1688275683149",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Pasantias de la beca",
"location": "Palermo-Italia",
"link": "http://www.facebook.com/album.php?aid=2094132&id=1125405534",
"cover_photo": "1688275843153",
"privacy": "friends",
"count": 45,
"type": "normal",
"created_time": "2010-12-06T18:27:02+0000",
"updated_time": "2011-03-26T18:56:36+0000"
},
{
"id": "1595209916563",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Profile Pictures",
"link": "http://www.facebook.com/album.php?aid=2082673&id=1125405534",
"cover_photo": "1579272118128",
"privacy": "friends",
"count": 3,
"type": "profile",
"created_time": "2010-10-07T16:12:55+0000",
"updated_time": "2011-04-10T04:17:28+0000"
},
{
"id": "1219601286582",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Mobile Uploads",
"link": "http://www.facebook.com/album.php?aid=2036068&id=1125405534",
"cover_photo": "1219601326583",
"privacy": "custom",
"count": 46,
"type": "mobile",
"created_time": "2009-09-13T01:38:31+0000",
"updated_time": "2011-04-10T04:18:10+0000"
},
{
"id": "1167780671099",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Nueva vieja tabla",
"location": "Playa hedionda",
"link": "http://www.facebook.com/album.php?aid=2027360&id=1125405534",
"cover_photo": "1167782391142",
"privacy": "custom",
"count": 12,
"type": "normal",
"created_time": "2009-06-21T01:37:13+0000",
"updated_time": "2011-04-10T04:19:13+0000"
},
{
"id": "1104082278679",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Cuya",
"description": "RLST & SRT",
"location": "Cuyagua",
"link": "http://www.facebook.com/album.php?aid=2018540&id=1125405534",
"cover_photo": "1104091558911",
"privacy": "custom",
"count": 30,
"type": "normal",
"created_time": "2009-03-01T23:49:23+0000",
"updated_time": "2011-04-10T04:16:34+0000"
},
{
"id": "1057859363135",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Boca de Aroa",
"link": "http://www.facebook.com/album.php?aid=2010172&id=1125405534",
"cover_photo": "1057880363660",
"privacy": "custom",
"count": 10,
"type": "normal",
"created_time": "2008-11-05T01:16:46+0000",
"updated_time": "2011-04-10T04:16:25+0000"
},
{
"id": "1051327359839",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "2da mojada",
"location": "Playa fantasma, via tucacas",
"link": "http://www.facebook.com/album.php?aid=2008928&id=1125405534",
"cover_photo": "1051329159884",
"privacy": "custom",
"count": 11,
"type": "normal",
"created_time": "2008-10-15T00:58:48+0000",
"updated_time": "2011-04-10T04:16:15+0000"
},
{
"id": "1050703064232",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Estrenando tabla",
"location": "Patanemo",
"link": "http://www.facebook.com/album.php?aid=2008822&id=1125405534",
"cover_photo": "1050705184285",
"privacy": "custom",
"count": 13,
"type": "normal",
"created_time": "2008-10-12T22:29:46+0000",
"updated_time": "2011-04-10T04:19:22+0000"
},
{
"id": "1037750900436",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Surfing Safari",
"location": "Choroni",
"link": "http://www.facebook.com/album.php?aid=2006692&id=1125405534",
"cover_photo": "1037753740507",
"privacy": "custom",
"count": 48,
"type": "normal",
"created_time": "2008-08-24T23:28:22+0000",
"updated_time": "2011-05-08T23:56:24+0000"
},
{
"id": "1032278323625",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "El Acto",
"location": "Valencia",
"link": "http://www.facebook.com/album.php?aid=2006001&id=1125405534",
"cover_photo": "1037921944712",
"privacy": "custom",
"count": 23,
"type": "normal",
"created_time": "2008-08-06T02:43:55+0000",
"updated_time": "2011-05-09T00:03:05+0000"
},
{
"id": "1031740150171",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "LA Marcha",
"location": "UC",
"link": "http://www.facebook.com/album.php?aid=2005922&id=1125405534",
"cover_photo": "1031754630533",
"privacy": "custom",
"count": 33,
"type": "normal",
"created_time": "2008-08-04T03:00:19+0000",
"updated_time": "2011-05-07T18:04:21+0000"
},
{
"id": "1029393851515",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Defensa de la Tesis",
"description": "20pts!!!",
"location": "Valencia",
"link": "http://www.facebook.com/album.php?aid=2005543&id=1125405534",
"cover_photo": "1029395531557",
"privacy": "custom",
"count": 27,
"type": "normal",
"created_time": "2008-07-26T02:27:55+0000",
"updated_time": "2011-05-07T09:03:28+0000"
},
{
"id": "1021161285706",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Rumbas, Playa y demas",
"link": "http://www.facebook.com/album.php?aid=2004373&id=1125405534",
"cover_photo": "1021163805769",
"privacy": "custom",
"count": 52,
"type": "normal",
"created_time": "2008-06-17T00:35:03+0000",
"updated_time": "2011-04-12T03:16:11+0000"
},
{
"id": "1013076403589",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Solo en Venezuela",
"link": "http://www.facebook.com/album.php?aid=2003189&id=1125405534",
"cover_photo": "1013077843625",
"privacy": "custom",
"count": 60,
"type": "normal",
"created_time": "2008-05-03T16:58:49+0000",
"updated_time": "2011-04-10T04:14:03+0000"
},
{
"id": "1010917109608",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Algunas Varias",
"link": "http://www.facebook.com/album.php?aid=2002900&id=1125405534",
"cover_photo": "1010917909628",
"privacy": "custom",
"count": 17,
"type": "normal",
"created_time": "2008-04-21T05:53:03+0000",
"updated_time": "2011-04-10T04:15:01+0000"
},
{
"id": "1008495809077",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "Solo para el video",
"description": "Este albun solo es para el video...",
"link": "http://www.facebook.com/album.php?aid=2002571&id=1125405534",
"cover_photo": "1008495969081",
"privacy": "custom",
"count": 15,
"type": "normal",
"created_time": "2008-04-06T05:32:57+0000",
"updated_time": "2011-04-06T21:32:12+0000"
},
{
"id": "1008464688299",
"from": {
"name": "Domingo Sarmiento Lupo",
"id": "1125405534"
},
"name": "PhotoShop",
"description": "Algunos trabajos de PhotoShop. Sugerencias?",
"link": "http://www.facebook.com/album.php?aid=2002568&id=1125405534",
"cover_photo": "1041428912384",
"privacy": "custom",
"count": 42,
"type": "normal",
"created_time": "2008-04-05T23:00:03+0000",
"updated_time": "2011-05-09T21:08:06+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/me/albums?access_token=2227470867|2.AQCF2qmOAjCq-I1Y.3600.1305979200.0-1125405534|4Fni91SEYdaFvpF5AscYzuVBwEg&limit=25&since=1304684490",
"next": "https://graph.facebook.com/me/albums?access_token=2227470867|2.AQCF2qmOAjCq-I1Y.3600.1305979200.0-1125405534|4Fni91SEYdaFvpF5AscYzuVBwEg&limit=25&until=1207436403"
}
}
我想回显每个块中的每个ID和NAME,但我在json时非常新,当我需要读取多个数据时,我总是卡住。谢谢你的帮助。
答案 0 :(得分:4)
假设您显示的JSON对象被指定为JS变量o
,并且您希望在JS中循环:
for (i = 0; i < o.data.length; i++) {
alert(
'id = ' + o.data[i].id + '\n' +
'name = ' + o.data[i].name + '\n' +
'from.id = ' + o.data[i].from.id + '\n' +
'from.name = ' + o.data[i].from.name
);
}
假设您显示的JSON对象在PHP字符串变量$s
中分配:
$aObject = json_decode($s, true);
foreach ($aObject['data'] as $i => $a) {
echo
'id = ' . $a['id'] . "<br />\n" .
'name = ' . $a['name'] . "<br />\n" .
'from.id = ' . $a['from']['id'] . "<br />\n" .
'from.name = ' . $a['from']['name'] . "<br /><br />\n\n";
}
答案 1 :(得分:1)
btw id 和名称,我在谈论:data
内部或data.from
内部
var jsondata = {"data": [{ // given string }]};
for (var i=0; i<json.data.length; i++) {
document.write("<hr/><b>"+i+"</b><br/>");
document.write("Data ID=>" + json.data[i].id+";Data Name=> " + json.data[i].name + "<br/>");
document.write(i+"From ID: " + json.data[i].from.id+";From Name:" + json.data[i].from.name );
}
$decodejson = json_decode($jsondata, true);
foreach ($decodejson ['data'] as $k => $v) {
echo
'id =>' . $v['id'] . "<br />\n" .
'name => ' . $v['name'] . "<br />\n" .
'from.id =>' . $v['from']['id'] . "\n" .
'from.name => ' . $v['from']['name'] . "\n";
}
答案 2 :(得分:0)