【问题标题】:Parse non array JSON with php用 php 解析非数组 JSON
【发布时间】:2014-05-20 22:56:35
【问题描述】:

我正在用 php+codeigniter 开发一个后端,它连接到返回 JSON 的数据提供服务,如下所示:

{
"code": "36397_26320",
"type": "TEAMS",
"4522": {
    "id": "4522",
    "code": "324",
    "name": "IT24354"
},
"4524": {
    "id": "4524",
    "code": "1234",
    "name": "IT24234"
},
"4527": {
    "id": "4527",
    "code": "2134",
    "name": "IT2678"
},
"4529": {
    "id": "4529",
    "code": "653",
    "name": "IT3546",
    "info":{
         "type1":
         {
            "type":"1",
            "url":"www.someurl.com",
            "date":"some date"
         },
         "type2":
         {
            "type":"2",
            "url":"www.someurl.com",
            "date":"some date"
         }

      }
},
"4530": {
    "id": "4530",
    "code": "3456",
    "name": "IT8769"
},
"4534": {
    "id": "4534",
    "code": "6453",
    "name": "IT3456"
},
"4537": {
    "id": "4537",
    "code": "76856",
    "name": "IT2676"
},
"4540": {
    "id": "4540",
    "code": "5768",
    "name": "IT23454"
},
"16225": {
    "id": "16225",
    "code": "4675",
    "name": "IT90687"
}
}

我想获取这些数字标识符中的信息,以便输出的 JSON 编码如下所示:

{
"items": [
    {
        "id": "4522",
        "code": "324",
        "name": "IT24354"
    },
    {
        "id": "4524",
        "code": "1234",
        "name": "IT24234"
    },
    {
        "id": "4527",
        "code": "2134",
        "name": "IT2678"
    },
    {
        "id": "4529",
        "code": "653",
        "name": "IT3546",
        "info":[
         {
            "type":"1",
            "url":"www.someurl.com",
            "date":"some date"
         },
         {
            "type":"2",
            "url":"www.someurl.com",
            "date":"some date"
         }

         ]
    },
    {
        "id": "4530",
        "code": "3456",
        "name": "IT8769"
    },
    {
        "id": "4534",
        "code": "6453",
        "name": "IT3456"
    },
    {
        "id": "4537",
        "code": "76856",
        "name": "IT2676"
    },
    {
        "id": "4540",
        "code": "5768",
        "name": "IT23454"
    },
    {
        "id": "16225",
        "code": "4675",
        "name": "IT90687"
    }
]
}

我的问题是,例如,我不知道该项目是否具有“信息”字段,例如项目 4529,或者在项目标识符级别,有两个字段称为代码和类型,没有更多信息.

有没有简单的方法来做这样的操作?还是为我想要的唯一方法执行尽可能多的级别?如何识别 json 中的键是否包含更多键值对?

谢谢大家!

【问题讨论】:

  • 创建一个函数来遍历数组。使用gettype($var) 测试object/array,如果是对象/数组,再次调用该函数。只需要几行代码。
  • 您可以使用 json_decode 并将额外参数设置为 true 将您的 JSON 转换为 PHP 数组,并使用 foreach 通过它进行交互。请参阅 PHP 手册了解如何使用 json_decode。从那里您可以轻松地使用例如isset 来了解天气info 是否存在于该项目中,if (isset($item['info'])) { //bla bla }is_array 以了解项目是否是一个数组。
  • @Ohgodwhy 如您所见,该 json 中没有对象数组。是否有任何函数可以确定它是否具有多个元素?

标签: php json codeigniter


【解决方案1】:

在本示例中,将您提供的第一个 json 对象放入 $json 变量中:

<?php 

$json = '{
"code": "36397_26320",
"type": "TEAMS",
"4522": {
    "id": "4522",
    "code": "324",
    "name": "IT24354"
},
"4524": {
    "id": "4524",
    "code": "1234",
    "name": "IT24234"
},
"4527": {
    "id": "4527",
    "code": "2134",
    "name": "IT2678"
},
"4529": {
    "id": "4529",
    "code": "653",
    "name": "IT3546",
    "info":{
         "type1":
         {
            "type":"1",
            "url":"www.someurl.com",
            "date":"some date"
         },
         "type2":
         {
            "type":"2",
            "url":"www.someurl.com",
            "date":"some date"
         }

      }
},
"4530": {
    "id": "4530",
    "code": "3456",
    "name": "IT8769"
},
"4534": {
    "id": "4534",
    "code": "6453",
    "name": "IT3456"
},
"4537": {
    "id": "4537",
    "code": "76856",
    "name": "IT2676"
},
"4540": {
    "id": "4540",
    "code": "5768",
    "name": "IT23454"
},
"16225": {
    "id": "16225",
    "code": "4675",
    "name": "IT90687"
}
}';
?>

现在我们可以创建一个 json 对象来访问数据:

$data = json_decode($json);

允许我们遍历该数据对象并查看是否设置了info

foreach($data as $item){
    if(isset($item->info)){
        // info found...do what you need to...
        print $item->id . " <br />";print_r($item->info);
    }
}

保证退货:

4529 
stdClass Object
(
    [type1] => stdClass Object
        (
            [type] => 1
            [url] => www.someurl.com
            [date] => some date
        )

    [type2] => stdClass Object
        (
            [type] => 2
            [url] => www.someurl.com
            [date] => some date
        )

)



Example

【讨论】:

  • 谢谢@Darren 我实际上最终使用了 isset 函数来分析 json 中的值。
  • @RobertoNovelo 很高兴 :)
【解决方案2】:

非常感谢大家,这有点棘手,但我最终做了以下,因为它允许我在不知道键值是否是另一个 JSON 的情况下探索完整的 json。现在它返回相同的输入 json,但是要将输出更改为数组,只需在递归时将括号添加到返回值

$response[$key][] = $this->read_non_array_json(json_encode($value));

非常欢迎改进:

private function read_non_array_json($data)
{

    $json = json_decode($data);

    if(is_object($json))
    {
        if(isset($json->id))
        {
            //obtain values, use a global array maybe to save info or something...
                        $id = $json->id;
                        $code = $json->code;
                        $name = $json->name;
        }
        if(isset($json->info)
        {
              //more code...
        }

        foreach($json as $key => $value)
        {
            if(is_object($value))
            {
                $response[$key]= $this->read_non_array_json(json_encode($value));
            }
            else
            {
                $response[$key] = $value;
            }

        }

        return $response;

    }
    else
    {

        return $data;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多