【问题标题】:Reading Json object with inside array使用内部数组读取 Json 对象
【发布时间】:2017-05-12 05:49:51
【问题描述】:

这是我的 JSON 结果:

{
  "@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"
    }
  ]
}

在 PHP 中我进入

$response = json_decode($aboveresult);

但我的问题是值在数组中。我想同时获取数组值,如 id、modelId、Name、... 请帮我。 我试过 $response['value']。但它显示错误,如 Cannot use of type stdClass as array

【问题讨论】:

    标签: php json


    【解决方案1】:

    json_decode() 接受第二个参数,默认为false。如果您传递true,该函数将返回一个关联的array,而不是stdClass 的实例,您可以按照以前尝试的方式使用它。

    【讨论】:

      【解决方案2】:

      你必须改变:

      $response = json_decode($aboveresult,true);
      

      当您提到第二个参数为true 时,您将获得ASSOCIATIVE 数组

      【讨论】:

      • 是的正确。我试过了。但是那个时候出现了一些其他错误,让我很困惑。谢谢
      【解决方案3】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多