使用 PHP 读取 JSON 数据

2021-12-30 00:00:00 json solr php

Solr 以以下 JSON 格式返回响应.

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

  }}

使用 PHP 读取 student_id、student_name 的简单方法是什么?

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

推荐答案

使用 $obj = json_decode($yourJSONString); 将其转换为对象.

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

然后使用 foreach($obj->response->docs as $doc) 遍历docs".

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

然后您可以使用 $doc->student_id$doc->student_name[0] 访问这些字段.

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

相关文章