David K.
David K.

Reputation: 324

JSON returns empty on special characters in PHP?

I am trying to display some values from my JSON but fail on two whereas the rest works perfectly. I suspect it has something to do with the characters within, but have no idea on how to proceed.

Tried tricks:

  1. UTF-8-ing everything
  2. Regexing the JSON directly
  3. Decode, Encode with JSON_PRETTY_PRINT/JSON_UNESCAPED_UNICODE and Decode again
  4. and some more I can't remember anymore...

Any pointers on how to tackle this greatly appreciated:

var_dump of $json

{
    "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 Code

<?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/>';
}

Expected result

1
First Name
1
http://www.example.com/pv/?1&amp;NAME=[[first-name/1/?refID=1/first-name]]&amp;denk5=[[1]]
http://www.example.com/pc/?1&amp;NAME=[[first-name/1/?refID=1/first-name]]&amp;denk5=[[1]]
2
Second Name
2
http://www.example.com/pv/?1&amp;NAME=[[second-name/2/?refID=2/second-name]]&amp;denk5=[[2]]
http://www.example.com/pc/?1&amp;NAME=[[second-name/2/?refID=2/second-name]]&amp;denk5=[[2]]

What I get

1
First Name
1
 <- link1 empty
 <- link2 empty
2
Second Name
2
 <- link1 empty
 <- link2 empty

What am I missing?

EDIT @Brad solution works perfectly.

Upvotes: 0

Views: 75

Answers (2)

user1978142
user1978142

Reputation: 7948

Its not about the JSON data, you can access them properly this way. Consider this example:

$json_string = '{"products":{"product":[{"@id":"1","name":"First Name","fetchers":{"fetcher":[{"@fetcherId":"1","link1":"http:\/\/www.example.com\/pv\/?1&amp;NAME=[[first-name\/1\/?refID=1\/first-name]]&amp;denk5=[[1]]","link2":"http:\/\/www.example.com\/pc\/?1&amp;NAME=[[first-name\/1\/?refID=1\/first-name]]&amp;denk5=[[1]]"}]}},{"@id":"2","name":"Second Name","fetchers":{"fetcher":[{"@fetcherId":"2","link1":"http:\/\/www.example.com\/pv\/?1&amp;NAME=[[second-name\/2\/?refID=2\/second-name]]&amp;denk5=[[2]]","link2":"http:\/\/www.example.com\/pc\/?1&amp;NAME=[[second-name\/2\/?refID=2\/second-name]]&amp;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/>';
}


?>

Should yield something like this:

@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]]

Upvotes: 1

Brad
Brad

Reputation: 163603

Your problem has nothing to do with JSON, and nothing to do with character encoding. It's that you're not picking the right element. $data['fetchers']['fetcher'] is an array. If you want the first element, use [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/>';
}

Also, don't forget to use htmlspecialchars() around any arbitrary data used in the context of HTML.

Upvotes: 4

Related Questions