用php解析非数组JSON
我正在用php+codeigniter开发一个后端,该后端连接到一个返回JSON的数据提供服务,如下所示:用php解析非数组JSON,php,json,codeigniter,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": {
{
"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"
}
]
}
我的问题是,例如,我不知道项目是否有字段“info”,例如在项目4529中,或者在项目标识符级别,有两个名为code和type的字段没有进一步的信息
有什么简单的方法可以做这样的操作吗?或者说,我想要达到多少级别就执行一个foreach是唯一的方法吗?如何识别json中的键是否包含更多的键值对
谢谢大家! 获取您提供的第一个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"
}
}';
?>
允许我们浏览该数据对象并查看是否设置了info
:
foreach($data as $item){
if(isset($item->info)){
// info found...do what you need to...
print $item->id . " <br />";print_r($item->info);
}
}
非常感谢大家,这有点棘手,但我最后做了以下几点,因为它允许我在不知道键值是否是另一个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;
}
}
创建一个函数来遍历数组。使用
gettype($var)
测试object/array
,如果它是一个对象/数组,请再次调用该函数。这只需要几行代码。您可以使用json\u decode
,将额外参数设置为true
,将json转换为PHP数组,并使用foreach
通过它进行交互。有关如何使用json\u解码
,请参阅PHP手册。从那里,您可以很容易地使用例如isset
来了解该项中是否存在info
,if(isset($item['info']){//bla}
或is_array
来了解该项是否为数组。@ohgoodwhy正如您所注意的,该json中没有对象数组。是否有任何函数可以确定它是否有多个元素?谢谢@Darren我实际上最终使用isset函数来分析json中的值。@RobertoNovelo很高兴:)
$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;
}
}