user8001011
user8001011

Reputation:

Reading Json object with inside array

This is my JSON result:

{
  "@odata.context": "http://wabi-west-europe-redirect.analysis.windows.net/v1.0/collections/washington/workspaces/37380bc1-dd47-4c95-8dbd-5efecafc8b26/$metadata#reports",
  "value": [
    {
      "id": "6ea77895-f92a-4ca6-90f7-cdade3683cd6",
      "modelId": 0,
      "name": "america",
      "webUrl": "https://app.powerbi.com/reports/6ea77895-f92a-4ca6-90f7-cdade3683cd6",
      "embedUrl": "https://embedded.powerbi.com/appTokenReportEmbed?reportId=6ea77895-f92a-4ca6-90f7-cdade3683cd6",
      "isOwnedByMe": true,
      "isOriginalPbixReport": false,
      "datasetId": "3f1f480c-4a8c-4756-87eb-fc29f5d76de3"
    },
    {
      "id": "ce558be6-aaf9-4bee-b344-6db7754e572b",
      "modelId": 0,
      "name": "dency",
      "webUrl": "https://app.powerbi.com/reports/ce558be6-aaf9-4bee-b344-6db7754e572b",
      "embedUrl": "https://embedded.powerbi.com/appTokenReportEmbed?reportId=ce558be6-aaf9-4bee-b344-6db7754e572b",
      "isOwnedByMe": true,
      "isOriginalPbixReport": false,
      "datasetId": "5264cf84-214a-4c33-8f8e-f421d8ce1846"
    }
  ]
}

In PHP im getting into

$response = json_decode($aboveresult);

But My problem is the value is in array.I want to get both the array value like id,modelId,Name,... Please help me. I tried $response['value'].But its showing error like Cannot use object of type stdClass as array

Upvotes: 0

Views: 100

Answers (3)

lalithkumar
lalithkumar

Reputation: 3540

You have to change:

$response = json_decode($aboveresult,true);

When you mentioned the second parameter as true you will get the ASSOCIATIVE array

Upvotes: 0

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

Try this

echo "<pre>";
$json_data = json_decode($json);  //$json = your json string 


print_r($json_data->value);

foreach($json_data->value as $value) {
    echo 'ID: '.$value->id .'<br>';
    echo 'modelId: '.$value->modelId .'<br>';
    echo 'name: '.$value->name .'<br>';
}

Upvotes: 0

Tobias F.
Tobias F.

Reputation: 1048

json_decode() accepts a second parameter, which is by default false. If you pass true, the function will return you an associative array instead of an instance of stdClass and you can work with it the way you tried before.

Upvotes: 1

Related Questions