Read string json in php

Asked

Viewed 332 times

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’ve tried every example I could find, but none helped me. The last one I tried 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, I’d appreciate it!

  • 1

    You used json_deconde() before? show how you created $obj

2 answers

1


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

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

You can also use the print_r to print the object structure 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;

}

1

Utilize json_decode() to transform the string into an array informing the second argument as true, then do 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 it was better to take the return as 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
                                )

                        )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.