1

I'm receiving a JSON response from a url which on decoding returns null while decoding it directly from the string it produces gives results.

String returned from the url:

{"status_code":0,"response":{"names":"Bon Charles"},"status_message":"Succeeded."}

Result from URL on decoding: NULL Result on taking the produced string and hard-coding it into a string:

object(stdClass)#19 (3) { ["status_code"]=> int(0) ["response"]=> object(stdClass)#20 (1) { ["names"]=> string(15) "Bon Charles" } ["status_message"]=> string(16) "Succeeded." }

Below is the function that calls the url for the response:

public function checkLoginStatus() {
        $status = $this->postLoginsJSON();

        $values = json_decode($status);
        $json_string = '{"status_code":0,"response":{"names":"Bon Charles"},"status_message":"Login Succeeded."}';
        $a = json_decode($json_string);
        var_dump($values);//returns null
        var_dump($a);//returns values
    }

Kindly assist.

Kabs
  • 227
  • 2
  • 4
  • 12
  • Can't help much as you haven't said what `$this->postLoginsJSON()` does. The result of `$status` could be an object, an array, or anything. You could try converting it to a string. – owenvoke Feb 11 '16 at 11:33
  • please print the output of `var_dump($this->postLoginsJSON());` - most probably it does not generate valid JSON for some reason. – Franz Gleichmann Feb 11 '16 at 11:40
  • 1
    "It could be the encoding of the special characters. You could ask json_last_error() to get definite information." From http://stackoverflow.com/a/2410358/452708 – Abhijeet Feb 11 '16 at 11:58
  • Have you checked the returned JSON string at [JSONLint](http://jsonlint.com). At least you will know that it is valid and that the `json_decode` parameters will need to be `tweaked` (technical term ;-/). – Ryan Vincent Feb 11 '16 at 13:28
  • 1
    Hello, $this->postLoginsJSON() returns the below result:{ status_code: 0, response: { names: "Bon Charles" }, status_message: "Succeeded." } – Kabs Feb 12 '16 at 05:31

0 Answers0