我正在尝试从我的JSON中显示一些值,但在两个上失败,而其余的工作完美。 我怀疑它与内部的角色有关,但不知道如何继续。
尝试过技巧:
关于如何解决这个问题的任何指示都非常感激:
$ json的var_dump
{
"products":{
"product":[{
"@id":"1",
"name":"First Name",
"fetchers":{
"fetcher":[{
"@fetcherId":"1",
"link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]",
"link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]"
}]
}
},{
"@id":"2",
"name":"Second Name",
"fetchers":{
"fetcher":[{
"@fetcherId":"2",
"link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]",
"link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]"
}]
}
}]
}
}
PHP代码
<?php
$json = json_decode($json,true);
foreach($json['products']['product'] as $data) {
echo $data['@id'].'<br/>';
echo $data['name'].'<br/>';
echo $data['fetchers']['fetcher']['@fetcherId'].'<br/>';
echo $data['fetchers']['fetcher']['link1'].'<br/>';
echo $data['fetchers']['fetcher']['link2'].'<br/>';
}
预期结果
1
First Name
1
http://www.example.com/pv/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
http://www.example.com/pc/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
2
Second Name
2
http://www.example.com/pv/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]
http://www.example.com/pc/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]
我得到了什么
1
First Name
1
<- link1 empty
<- link2 empty
2
Second Name
2
<- link1 empty
<- link2 empty
我错过了什么?
修改 @Brad解决方案非常有效。
答案 0 :(得分:4)
您的问题与JSON无关,与字符编码无关。你没有选择正确的元素。 $data['fetchers']['fetcher']
是一个数组。如果您想要第一个元素,请使用[0]
。
foreach($json['products']['product'] as $data) {
echo $data['@id'].'<br/>';
echo $data['name'].'<br/>';
echo $data['fetchers']['fetcher'][0]['@fetcherId'].'<br/>';
echo $data['fetchers']['fetcher'][0]['link1'].'<br/>';
echo $data['fetchers']['fetcher'][0]['link2'].'<br/>';
}
另外,不要忘记在HTML上下文中使用任意数据htmlspecialchars()
。
答案 1 :(得分:1)
它不是关于JSON数据,您可以通过这种方式正确访问它们。考虑这个例子:
$json_string = '{"products":{"product":[{"@id":"1","name":"First Name","fetchers":{"fetcher":[{"@fetcherId":"1","link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]","link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[first-name\/1\/?refID=1\/first-name]]&denk5=[[1]]"}]}},{"@id":"2","name":"Second Name","fetchers":{"fetcher":[{"@fetcherId":"2","link1":"http:\/\/www.example.com\/pv\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]","link2":"http:\/\/www.example.com\/pc\/?1&NAME=[[second-name\/2\/?refID=2\/second-name]]&denk5=[[2]]"}]}}]}}';
$json_data = json_decode($json_string, true);
$data = array();
foreach($json_data['products']['product'] as $key => $value) {
echo '@id => ' . $value['@id'] . '<br/>';
echo 'name => ' .$value['name'] . '<br/>';
if(isset($value['fetchers'])) {
$fetchers = reset($value['fetchers']['fetcher']); // <-- set pointer to first which is zero index
echo 'fetcher_id => ' . $fetchers['@fetcherId'] . '<br/>';
echo 'link1 => ' . $fetchers['link1'] . '<br/>';
echo 'link2 => ' . $fetchers['link2'] . '<br/>';
}
echo '<br/>';
}
?>
应该产生这样的东西:
@id => 1
name => First Name
fetcher_id => 1
link1 => http://www.example.com/pv/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
link2 => http://www.example.com/pc/?1&NAME=[[first-name/1/?refID=1/first-name]]&denk5=[[1]]
@id => 2
name => Second Name
fetcher_id => 2
link1 => http://www.example.com/pv/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]
link2 => http://www.example.com/pc/?1&NAME=[[second-name/2/?refID=2/second-name]]&denk5=[[2]]