使用php解析非数组JSON

时间:2014-05-20 22:56:36

标签: php json codeigniter

我正在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,或者还有项目标识符级别,有两个字段称为代码和类型,没有进一步的信息

有没有简单的方法来进行此类操作?或者是按照我想获得的唯一方式执行多个级别的foreach?如何识别json中的键是否包含更多键值对?

谢谢大家!

2 个答案:

答案 0 :(得分:0)

获取您提供的第一个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

答案 1 :(得分:0)

非常感谢你,这有点棘手但我最终做了以下工作,因为它允许我在不知道键值是否是另一个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;
    }
}