这是我的JSON文件:
{
"user": {
"id": "2.8.388387",
"category": "posts",
"json_metadata": {
"tags": [
"new",
],
"image": [
"https://s32.postimg.org/4twcn4yrp/13918652_1345543288792650_1255274463_o.gif"
]
}
}
}
和PHP:
$json = json_decode($content, true);
foreach($json as $i){
$id = $i['id'];
$category = $i['category'];
$image = $i['json_metadata']['image'];
echo $id;
echo $category;
echo $image;
}
Echo $ id和$ category工作正常。但是,$ image错误: 警告:非法字符串偏移'图像'
任何想法我会错过什么?
答案 0 :(得分:0)
试试此代码
$content = '{
"user": {
"id": "example glossary",
"category": "example glossary",
"json_metadata": {
"tags": ["new"],
"image": [
"https://s32.postimg.org/4twcn4yrp/13918652_1345543288792650_1255274463_o.gif"
]
}
}
}';
$json = json_decode($content, true);
print_r($json['user']['json_metadata']['image'][0]);
你会得到:
https://s32.postimg.org/4twcn4yrp/13918652_1345543288792650_1255274463_o.gif
答案 1 :(得分:0)
json_metadata是循环内的数组,因此您只需要像在数组中一样从数组中获取数据。它很简单......这将现在循环使用。你也可以在循环中打印json_metadata数组。
$content = '{
"user": {
"id": "example glossary",
"category": "example glossary",
"json_metadata": {
"tags": ["new"],
"image": [
"https://s32.postimg.org/4twcn4yrp/13918652_1345543288792650_1255274463_o.gif"
]
}
}
}';
$json = json_decode($content, true);
foreach ($json as $i) {
$id = $i['id'];
$category = $i['category'];
$image = $i['json_metadata']['image'][0];
echo $id;
echo $category;
echo $image;
}