使用 PHP 读取 JSON 数据

发布于 2024-11-04 09:38:38 字数 610 浏览 0 评论 0原文

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

{
  "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的简单方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栩栩如生 2024-11-11 09:38:38

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

然后使用 foreach($obj->response->docs as $doc) 迭代“docs”。

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

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

-残月青衣踏尘吟 2024-11-11 09:38:38

PHP 有一个 json_decode 函数,它允许您将 JSON 字符串转换为数组:

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

当然,您可能想要迭代学生列表而不是访问索引 0。

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.

Saygoodbye 2024-11-11 09:38:38
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...
清浅ˋ旧时光 2024-11-11 09:38:38

为什么不只使用 Solr 的 PHP 客户端之一或 PHP 响应编写器?请参阅http://wiki.apache.org/solr/SolPHP

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

孤城病女 2024-11-11 09:38:38
$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>";
}
$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>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文