Reputation: 369
I am facing with a problem, when I am trying to parse json returns from server into array in php. Here is my code ...
<?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;
?>
This is the json after trimming space. I also want to convert it int array with json_decode() but no result return. I think this json is valid. And suggestion pls. This is my firstly trying to feed web service from server.
Thanks
'{
"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"
}
]
}'
----------Edit---------
As your suggestion I comment trimming space and correct json format. And echo $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"
}
]
}
In decoding array doesn't have any data.
$jdata = json_decode($data, true);
print_r $jdata;
echo "user status -> ". $jdata["status"];
when I copy that json and hard code in a string, decode it again, it works for me. please see my testing code....
$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;
Any suggestion pls!
Upvotes: 1
Views: 5572
Reputation: 16771
First of all your json is malformed. Remove the '' from the beginning and the end of your file. The contents of $data should look like this:
{
"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"
}
]
}
Second $jdata is an associative array. You cannot print its contents with echo. Instead do
print_r($jdata);
Third you don't need to remove spaces. Do that in the script that produces the json, otherwise just parse the json with the spaces directly.
Upvotes: 1
Reputation: 1745
Try this
$jdata = json_decode($trimspace, true);
print_r($jdata);
Upvotes: 1
Reputation: 2201
json_decode
usually returns an object
, so I don't think your code is wrong here.
$arrayObject = new ArrayObject($object);
$array = $arrayObject->getArrayCopy();
This is how you can convert it to an array
. It works in PHP 5.3+
Upvotes: 1
Reputation: 1222
I think your json is malformed. Remove $data="'".$data."'";
You can check json error if any.
And $trimspace = preg_replace('/\s+/', '', $data);
is needless.
Upvotes: 3