Последовательное чтение формата json

#php #json

#php #json

Вопрос:

У меня есть это содержимое json внутри переменной в php:

 {
   "data": [
      {
         "id": "1977103383661",
         "from": {
            "name": "Domingo Sarmiento Lupo",
            "id": "1125405534"
         },
         "name": "MUM Invasion Photos",
         "link": "http://www.facebook.com/album.php?aid=2117387amp;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=2115307amp;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=2108888amp;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 iPhoneu00ae",
         "description": ""PhotoAge for iPhoneu00ae" por Percipo Inc. Haciendo computadoras mas humano...",
         "link": "http://www.facebook.com/album.php?aid=2106226amp;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=2097024amp;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=2096857amp;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=2094132amp;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=2082673amp;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=2036068amp;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=2027360amp;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 amp; SRT",
         "location": "Cuyagua",
         "link": "http://www.facebook.com/album.php?aid=2018540amp;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=2010172amp;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=2008928amp;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=2008822amp;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=2006692amp;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=2006001amp;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=2005922amp;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=2005543amp;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=2004373amp;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=2003189amp;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=2002900amp;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=2002571amp;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=2002568amp;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|4Fni91SEYdaFvpF5AscYzuVBwEgamp;limit=25amp;since=1304684490",
      "next": "https://graph.facebook.com/me/albums?access_token=2227470867|2.AQCF2qmOAjCq-I1Y.3600.1305979200.0-1125405534|4Fni91SEYdaFvpF5AscYzuVBwEgamp;limit=25amp;until=1207436403"
   }
}
  

Я хочу повторить каждый идентификатор и ИМЯ из каждого блока, но я очень новичок в json, и я всегда застреваю, когда мне нужно прочитать более одного данные. Спасибо за любую помощь.

Комментарии:

1. Пожалуйста, покажите, как объект JSON передается в переменную PHP и как он там выглядит.

2. Это недопустимый JSON. В допустимом JSON, / должно быть экранировано внутри строк .

Ответ №1:

Предполагается, что показанный вами объект 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 />nn";
}
  

Комментарии:

1. Упс, извините, видел только нотацию JSON, которую невозможно определить таким образом в PHP.

2. Обновил мой ответ, добавил цикл PHP.

3. Телен, превзойди меня, 1 за скорость и точность

Ответ №2:

кстати, о каких идентификаторе и имени вы говорите: внутри data или внутри data.from

Способ Javascript

 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 );
}
  

ДЕМОНСТРАЦИЯ

PHP способ

 $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";
}
  

Комментарии:

1. @jurgen Спасибо 🙂 что мне нужно изменить?? я не получил u

2. Ваш код верен. Я имел в виду, что оба наших ответа были получены почти в одно и то же время (близко друг к другу). Я не являюсь носителем языка, пожалуйста, потерпите меня.

Ответ №3: