gpt4 book ai didi

php - 使用 PHP 解析 JSON 数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:24 24 4
gpt4 key购买 nike

这是一个有点菜鸟的问题。我正在处理一个看起来像这样的 JSON 文件:

{"nodes": 
[{"node":
{"Vocabulary name":"Bestseller Format",
"Term":"Hardcover Fiction",
"Bestseller1":"9780470227800",
"Bestseller2":"9781451617962",
"Bestseller3":"9781439167397",
"Bestseller4":"9781617750106",
"Bestseller5":"9780385533300",
"Bestseller6":"9780670022526",
"Bestseller7":"9781609530358",
"Bestseller8":"9780132358040",
"Bestseller9":"9780596159771",
"Bestseller10":"9780151014163",
"Bestseller11":"9780393334807",
"Bestseller12":"9780805090161"}
}]
}

我正在尝试用 php 解析它。我想出了一个脚本




      $jsonurl = "http://www.xtracrunchy.com/pandpnewlook/?q=pandp/bestsellers";
$json = file_get_contents($jsonurl,NULL, NULL, 0, 1000);
$json_output = json_decode($json,true);

$bestseller_array = $json_output[节点][0][节点];

foreach ($bestseller_array as $key => $value)
{
打印$键;
打印 ” - ”;
打印$值;
打印“
”;
}

?>

我希望最终能够构建畅销书 ISBN 的有序列表,但运行此脚本时我得到的唯一输出是

词汇名称-畅销书格式术语 - 精装小说

当我添加 print count($bestseller_array);我只得到数组中的 2 个元素。

如有任何帮助,我们将不胜感激。

最佳答案

首先,我强烈建议您使用 CURL 而不是 file_get_contents(),因为在 some occasions 中, 它不会起作用。但是因为这不是问题的一部分,所以我不会再谈论这个了。

其次,您正在使用未命名的文字常量,这非常糟糕。

$json_output[nodes][0][node];

应该是:

$json_output['nodes'][0]['node'];

参见 documentation here on why .

最后,您在上面编写的 JSON 字符串与该 url 上返回的内容不匹配,并且脚本在这个"new"json 上确实按预期工作。

关于php - 使用 PHP 解析 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5797428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com