12
votes

Lire les données JSON en utilisant php

Solr renvoie la réponse au format JSON suivant.

{
  "responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "indent":"on",
      "start":"0",
      "q":"*:*",
      "wt":"json",
      "version":"2.2",
      "rows":"10"}},
  "response":{"numFound":3,"start":0,"docs":[
      {
        "student_id":"AB1001",
        "student_name":[
          "John"]
    },
      {
        "student_id":"AB1002",
        "student_name":[
          "Joe"]
    },
      {
        "student_id":"AB1003",
        "student_name":[
          "Lorem"]
    }]

  }}


2 commentaires

Dupliqué possible de Comment lire ce Json en utilisant php?


Votre JSON n'est pas valide. Il y a un ] manquant avant le dernier }}


5 Réponses :


2
votes
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...

0 commentaires

17
votes

Utilisez $ obj = json_decode ($ yourjsonstring); pour le convertir en objet.

Puis utilisez foreach ($ obj-> réponse-> docs comme $ doc) à itérer sur les "docs".

Vous pouvez ensuite accéder aux champs à l'aide de $ doc-> étudiant_id et $ DOC-> Nom de l'étudiant [0] . .


0 commentaires

10
votes

php a une fonction JSON_Decode qui vous permettra de tourner une chaîne JSON en une matrice:

$array = json_decode($json_string, true);
$student_id = $array['response']['docs'][0]['student_id'];
...


0 commentaires

0
votes

Pourquoi ne pas simplement utiliser l'un des clients PHP pour SOLR ou l'auteur de réponse PHP? Voir http://wiki.apache.org/solr/solphp


0 commentaires

0
votes
$json_a = json_decode($string, TRUE);
$json_o = json_decode($string);


#array method
foreach($json_a['response']['docs'] as $students)
{
    echo $students['student_id']." name is ".$students['student_name'][0];
    echo "<br>";
}

#Object Method`enter code here`
foreach($json_o->response->docs as $sthudent_o)
{
    echo $sthudent_o->student_id. " name is ".$sthudent_o->student_name[0];
    echo "<br>";
}

0 commentaires