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

时间:2013-04-07 03:42:22

标签: php foreach json arrays

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

PHP代码: -

1)没有工作

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

2)仅获取输出“data Array”但未获得对象详细信息

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

之后我还尝试用print_r和var_dump打印细节。

var_dump的结果是: -

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"
    }
  }
}

我试图找到我犯错的地方,但没有任何想法。如果有人有任何想法,请指导我。

2 个答案:

答案 0 :(得分:2)

您显然将JSON数据解码为数组,而不是将解码为对象json_decode(..., true)返回数组,如果省略true参数则返回对象。更改代码以期望数组或更改解码以返回对象。

答案 1 :(得分:0)

array.php

<?php
    // $json = json_decode($json_data, true);
    $json = array(
        'data' => array(
            array(
                'bussiness_type' => 'bar',
                'bussiness_name' => 'KKK',
                'number' => '1234567',
                'bussiness_id' => 'test1'
            ),
            array(
                'bussiness_type' => 'restauratns',
                'bussiness_name' => 'KKKK',
                'number' => '1234567',
                'bussiness_id' => 'test2'
            )
         )
    );

    /*
        array(1) {
          ["data"]=>
          array(2) {
            [0]=>
            array(4) {
              ["bussiness_type"]=>
              string(3) "bar"
              ["bussiness_name"]=>
              string(3) "KKK"
              ["number"]=>
              string(7) "1234567"
              ["bussiness_id"]=>
              string(5) "test1"
            }
            [1]=>
            array(4) {
              ["bussiness_type"]=>
              string(11) "restauratns"
              ["bussiness_name"]=>
              string(4) "KKKK"
              ["number"]=>
              string(7) "1234567"
              ["bussiness_id"]=>
              string(5) "test2"
            }
          }
        }
    */

    foreach( $json['data'] as $value )
    {
        echo $value['bussiness_type'] . "\n" 
            . $value['bussiness_name'] . "\n" 
            . $value['number'] . "\n" 
            . $value['bussiness_id'] . "\n\n";
    }
?>

object.php

<?php
    // $json = json_decode($json_data, true);
    $json = array(
        'data' => array(
            array(
                'bussiness_type' => 'bar',
                'bussiness_name' => 'KKK',
                'number' => '1234567',
                'bussiness_id' => 'test1'
            ),
            array(
                'bussiness_type' => 'restauratns',
                'bussiness_name' => 'KKKK',
                'number' => '1234567',
                'bussiness_id' => 'test2'
            )
         )
    );

    $to_json = json_encode($json);
    $to_obj = json_decode($to_json);
    /*
        object(stdClass)#1 (1) {
          ["data"]=>
          array(2) {
            [0]=>
            object(stdClass)#2 (4) {
              ["bussiness_type"]=>
              string(3) "bar"
              ["bussiness_name"]=>
              string(3) "KKK"
              ["number"]=>
              string(7) "1234567"
              ["bussiness_id"]=>
              string(5) "test1"
            }
            [1]=>
            object(stdClass)#3 (4) {
              ["bussiness_type"]=>
              string(11) "restauratns"
              ["bussiness_name"]=>
              string(4) "KKKK"
              ["number"]=>
              string(7) "1234567"
              ["bussiness_id"]=>
              string(5) "test2"
            }
          }
        }
    */

    foreach( $to_obj->data as $value )
    {
        echo $value->bussiness_type . "\n" 
            . $value->bussiness_name . "\n" 
            . $value->number . "\n" 
            . $value->bussiness_id . "\n\n";
    }

?>