dtj2ww9500 2013-04-07 03:42
浏览 78
已采纳

在php中解析json无法正常工作

{
    "data": [
        {
            "business_type": "bar",
            "business_name": "KKK",
            "number": "1234567",
            "business_id": "test1"
        },
        {
            "business_type": "restauratns",
            "business_name": "KKKK",
            "number": "1234567",
            "business_id": "test2"
        }
    ]
}

PHP Code:-

1) Not Worked

foreach ($json_array->data as $obj) {
    echo $obj->business_name;
    echo $obj->business_type;
    echo $obj->business_id;
}

2) Only get the output "data Array" but didn't get the object details

foreach ($json_array as $name=>$value) {
    echo "$name: $value
";
    foreach ($value as $entry) {
        echo '  ' . $entry->business_type;
        echo '  ' . $entry->business_name;
        echo '  ' . $entry->business_id;
        echo '  ' . $entry->number;
    }
}

after this I also try to print details with print_r and var_dump.

Result of var_dump is :-

array(1) {
  ["data"]=>
  array(2) {
    [0]=>
    array(4) {
      ["business_type"]=>
      string(3) "bar"
      ["business_name"]=>
      string(3) "KKK"
      ["number"]=>
      string(7) "1234567"
      ["business_id"]=>
      string(5) "test1"
    }
    [1]=>
    array(4) {
      ["business_type"]=>
      string(11) "restauratns"
      ["business_name"]=>
      string(4) "KKKK"
      ["number"]=>
      string(7) "1234567"
      ["business_id"]=>
      string(5) "test2"
    }
  }
}

I am trying to find where I am making mistake but didn't get any idea. Please kindly guide me if anyone has any idea.

  • 写回答

2条回答 默认 最新

  • dszsajhd237437 2013-04-07 03:46
    关注

    You're very apparently decoding the JSON data as array, not as object. json_decode(..., true) returns arrays, if you leave out the true parameter it returns objects. Either change your code to expect arrays or change your decoding to return objects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料