Parse JSON Data with PHP(用 PHP 解析 JSON 数据)
问题描述
我已多次解析 JSON 数据,但由于某种原因,无法找到嵌套数据时要使用的正确语法.我正在尝试从此 JSON 解析资产",但无论我尝试什么,都会继续获得为 foreach() 提供的无效参数.
I have parsed JSON data numerous times but for some reason cannot find the correct syntax to use when the data is nested. I am trying to parse the "assets" from this JSON but continue to get a invalid argument supplied foreach() regardless of what I try.
"3435":{
"name":"COLO-1014-SJ1",
"nickname":"US-SJC-004",
"type":"Colocated Server",
"location":"San Jose:55 S Market",
"assets":{
"CPU":[
{
"model":"Intel E3 1270"
}
],
"Hard Drives":[
{
"model":"Western Digital 500GB RE4 ABYX SATA"
},
{
"model":"Western Digital 500GB RE4 ABYX SATA"
},
{
"model":"Kingston 240GB SSD"
}
],
"RAM":[
{
"model":"Super Talent 4GB DDR3 1333 ECC"
},
{
"model":"Super Talent 4GB DDR3 1333 ECC"
},
{
"model":"Super Talent 4GB DDR3 1333 ECC"
},
{
"model":"Super Talent 4GB DDR3 1333 ECC"
}
],
我希望它类似于......
I would expect it to be something along the lines of...
$json = json_decode($jsondata, true);
foreach ($json as $item)
{
foreach ($item['assets']->RAM as $asset)
{
echo $asset->model;
}
推荐答案
来自php官方文档:http://php.net/manual/fr/function.json-decode.php
第二个 func arg 用于关联数组返回.如果您更喜欢在对象上操作关联数组,则可以使用它.
The 2nd func arg is for assoc array return. You can use it if you prefer to manipulate assoc array over object.
但你实际上在循环中混合了数组和对象.
But you actually mix array and object in your loop.
如果您将参数保持在 TRUE
,请使用 $item['assets']['RAM']
If you keep the arg at TRUE
, please use $item['assets']['RAM']
这篇关于用 PHP 解析 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 PHP 解析 JSON 数据


基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01