Read JSON Data Using PHP

前端 未结 5 1118
暗喜
暗喜 2020-11-30 10:41

Solr returns response in following JSON format.

{
  \"responseHeader\":{
    \"status\":0,
    \"QTime\":2,
    \"params\":{
      \"indent\":\"on\",
      \         


        
相关标签:
5条回答
  • 2020-11-30 10:57

    Why not just use one of the PHP clients for Solr, or the PHP response writer? See http://wiki.apache.org/solr/SolPHP

    0 讨论(0)
  • 2020-11-30 11:11

    PHP has a json_decode function that will allow you to turn a JSON string into an array:

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

    Of course, you might want to iterate through the list of students instead of accessing index 0.

    0 讨论(0)
  • 2020-11-30 11:18
    $result = json_decode($result, true);
    $result['response']['docs'][0]['student_id'] ...
    
    0 讨论(0)
  • 2020-11-30 11:20

    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].

    0 讨论(0)
  • 2020-11-30 11:23
    $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 讨论(0)
提交回复
热议问题