It looks like you have forgot to add the surrounding curly braces around the JSON data. If your JSON data is invalid then the json_decode() function will not work correctly.
I have fixed your JSON code below and this now validates and meets the JSON standard.
{
"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"
}
]
}
}
}
If you ever need to check your JSON code you can use a validator such as http://jsonlint.com/
Secondly, your PHP code is also wrong:
$json = json_decode($jsondata, true);
foreach ($json as $item)
{
foreach ($item->assets->RAM as $asset)
{
echo $asset->model;
}
}
You have been trying to access the returned object as an array which will also cause issues with the foreach loop.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…