【问题标题】:How to work with this JSON, including array如何使用这个 JSON,包括数组
【发布时间】:2021-12-22 17:15:26
【问题描述】:

我尝试使用基本的、少量的参数。 JSON 代码,没有任何问题。

但现在我需要使用这个“高级”JSON,我有点迷路了

  {
   "code":"success",
   "username":"x",
   "nodes":[
      {
         "id":"68",
         "time":987
      },
      {
         "id":"69",
         "time":987
      }
   ]
}

当我尝试使用之前的代码将值放入 PHP 变量时,我无法获得 IDTIMECODESUCCESS 没有问题。

我使用的PHP代码:

$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = @file_get_contents($url);
$details = json_decode($json, TRUE);
// print_r($details);
echo $details[code];

谢谢你们!

【问题讨论】:

  • 这能回答你的问题吗? How to access JSON decoded array in PHP
  • echo $details['nodes'][0]['id'];
  • 谢谢!这是工作。请问您,我如何计算和打印出总数组的数量? "nodes":[ { "id":"68", "time":987 }, { "id":"69", "time":987 } ] 在这种情况下,我想得到数字 2

标签: php json


【解决方案1】:
<?php
$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = @file_get_contents($url);
$details = json_decode($json, TRUE);
if (is_array($details['nodes'])) {
    echo "node count: " . count($details['nodes']) . "<br />";
    foreach ($details['nodes'] as $node) {
        echo "id: " . $node['id'] . "<br />";
        echo "time: " . $node['time'] . "<br />";
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多