Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JSON Data Using PHP

Tags:

json

php

solr

Solr returns response in following JSON format.

{
  "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"]
    }]

  }}

What will be the simple way to read student_id, student_name using PHP?

like image 396
Aadi Avatar asked May 03 '11 11:05

Aadi


1 Answers

Use $obj = json_decode($yourJSONString); to convert it to an object.

Then use foreach($obj->response->docs as $doc) to iterate over the "docs".

You can then access the fields using $doc->student_id and $doc->student_name[0].

like image 147
ThiefMaster Avatar answered Sep 18 '22 12:09

ThiefMaster