Read string json in php

1

I need to read the file below:

string '{"object":"list","hasMore":true,"limit":10,"offset":0,"data":[{"city":{"object":"city","id":15873,"ibgeCode":"3550308","name":"São Paulo","districtCode":"00","district":"São Paulo","state":"SP"}},{"city":{"object":"city","id":15718,"ibgeCode":"5200050","name":"Abadia de Goiás","districtCode":"05","district":"Abadia de Goiás","state":"GO"}},{"city":{"object":"city","id":9853,"ibgeCode":"3100104","name":"Abadia dos Dourados","districtCode":"05","district":"Abadia dos Dourados","state":"MG"}},{"city":{"obj'... (length=1415)

I tried all the examples I found, but none helped me. The last one I tested was:

foreach($obj->data->city as $sthudent_o)
{
    echo $sthudent_o;
    echo $sthudent_o->id. " name is ".$sthudent_o->name[0];
    echo "<br>";
}

If anyone can help me, thank you!

    
asked by anonymous 07.11.2015 / 21:25

2 answers

1

First you have to transform the string into an object using json_decode( $string ) of PHP.

And now, in your code, the item of the object to be traversed is data and not city.

You can also use print_r to print the structure of the object and have a better view of how to manipulate it.

$obj = json_decode( $json );

//print_r( $obj );

foreach( $obj->data as $data ) {

    //print_r( $data );

    $dados = $data->city;

    echo 'ID: ' . $dados->id;
    echo '<br>Nome: ' . $dados->name;

}
    
07.11.2015 / 21:50
1

Use json_decode() to transform the string into an array by reporting the second argument as true , then make the foreach.

<?php


$str = '[{"object":"list","hasMore":true,"limit":10,"offset":0,"data":[{"city":{"object":"city","id":15873,"ibgeCode":"3550308","name":"São Paulo","districtCode":"00","district":"São Paulo","state":"SP"}},
                                                              {"city":{"object":"city","id":15718,"ibgeCode":"5200050","name":"Abadia de Goiás","districtCode":"05","district":"Abadia de Goiás","state":"GO"}},
                                                              {"city":{"object":"city","id":9853,"ibgeCode":"3100104","name":"Abadia dos Dourados","districtCode":"05","district":"Abadia dos Dourados","state":"MG"}}
                                                             ]}]';
$json = json_decode($str, true);

foreach($json[0]['data'] as $item){
    echo $item['city']['name'] .'<br>';
}

The array structure will look like this, so I thought I'd better get the return as an arry than an object.

Example - ideone

Array
(
    [0] => Array
        (
            [object] => list
            [hasMore] => 1
            [limit] => 10
            [offset] => 0
            [data] => Array
                (
                    [0] => Array
                        (
                            [city] => Array
                                (
                                    [object] => city
                                    [id] => 15873
                                    [ibgeCode] => 3550308
                                    [name] => São Paulo
                                    [districtCode] => 00
                                    [district] => São Paulo
                                    [state] => SP
                                )

                        )
    
07.11.2015 / 22:06