我正面临一个问题,当我试图解析json从服务器返回到php中的数组。这是我的代码......
<?php
mb_internal_encoding('UTF-8');
$url = 'http://localhost/busexpress/api/v1/mobile_user_register/mobile_user_register/retrieve.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
//$data="'".$data."'";
echo $data;
curl_close($ch);
//$trimspace = preg_replace('/\s+/', '', $data);
//echo $trimspace;
$jdata = json_decode($data, true);
print_r $jdata;
?>
这是修剪空间后的json。我也想用json_decode()转换它的int数组但没有结果返回。我认为这个json是有效的。并建议请。这是我第一次尝试从服务器提供Web服务。
由于
'{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}'
---------- ---------编辑
根据你的建议,我评论修剪空间并纠正json格式。并回显$ data; .....
{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}
在解码数组中没有任何数据。
$jdata = json_decode($data, true);
print_r $jdata;
echo "user status -> ". $jdata["status"];
当我将json和硬代码复制到字符串中时,再次对其进行解码,它对我有用。请看我的测试代码....
$data =' {"status":"1","mobile_user":[{"id":"1","name":"saa","phone_no":"09978784963","activate_code":"","deposit":"0","created":"2015-05-29 00:00:00","updated":"0000-00-00 00:00:00","status":"1"},{"id":"3","name":"ttr","phone_no":"090930499","activate_code":"","deposit":"0","created":"2015-06-01 00:00:00","updated":"0000-00-00 00:00:00","status":"0"}]}';
$data = json_decode($data,true);
$status = $data['status'];
$mobile_user = $data['mobile_user'];
$id = $mobile_user[0]["id"];
$name = $mobile_user[0]["name"];
echo "id -> ". $id ."<br>";
echo "name -> ". $name;
任何建议都可以!
答案 0 :(得分:2)
我认为你的json格格不入。删除$data="'".$data."'";
如果有的话,您可以查看json error。
$trimspace = preg_replace('/\s+/', '', $data);
是不必要的。
答案 1 :(得分:1)
试试这个
$jdata = json_decode($trimspace, true);
print_r($jdata);
答案 2 :(得分:0)
json_decode
通常会返回object
,因此我认为您的代码在这里不对。
$arrayObject = new ArrayObject($object);
$array = $arrayObject->getArrayCopy();
您可以将其转换为array
。它适用于PHP 5.3 +
答案 3 :(得分:0)
首先你的json格式不正确。删除&#39;&#39;&#39;&#39;从文件的开头和结尾。 $ data的内容应如下所示:
{
"status": "1",
"user": [
{
"id": "27",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "0d08ed",
"deposit": "0",
"created": "2015-06-0316:35:08",
"updated": "1110-11-3000:00:00",
"status": "0"
},
{
"id": "28",
"name": "kktt",
"phone_no": "1239293",
"activate_code": "fb4876",
"deposit": "0",
"created": "2015-06-0316:37:14",
"updated": "1000-01-0100:00:00",
"status": "0"
}
]
}
第二个$ jdata是一个关联数组。您无法使用echo打印其内容。而是做
print_r($jdata);
第三,你不需要删除空格。在生成json的脚本中执行此操作,否则只需直接用空格解析json。