1

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!

4 Answers 4

3

I think your json is malformed. Remove $data="'".$data."'";

You can check json error if any.

And $trimspace = preg_replace('/\s+/', '', $data); is needless.

Sign up to request clarification or add additional context in comments.

2 Comments

thank php.net/manual/en/function.json-last-error.php, this link helps me, the error I got is json syntax error, but still solving.
1

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+

Comments

1

Try this

  $jdata = json_decode($trimspace, true);
     print_r($jdata);

Comments

1

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.

2 Comments

@ Bjarnstroem, thank for your answer, I edited my question again, could you give me a favor of checking it agin.
@SAWAUNG Hey, I suggest you read the PHP documentation before posting here... print_r needs () around the parameters. So don't write print_r $jdata; but print_r($jdata);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.