JVE999
JVE999

Reputation: 3517

Are there any alternatives to json_decode?

I have a JSON string that passes the checks in http://www.freeformatter.com/json-validator.html, which returns:

The JSON input is valid in JavaScript.
The JSON input is valid according to RFC 4627 (JSON specfication).

However, when I run:

$string = json_decode($string);
print_r($string);

It prints the string exactly the same way it was before the json_decode($string).

Is there something else I can try? Or maybe something I don't know or didn't think about?

The string is here: http://pubapi.cryptsy.com/api.php?method=marketdatav2


To get the string, I was using:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=marketdatav2");
$ret = curl_exec($ch);

and then

$ret = substr($ret, 0,-1);

As it returned a 1 at the end, although I had originally tried it without removing the last character and got the same result.

Upvotes: 0

Views: 5859

Answers (2)

qwertmax
qwertmax

Reputation: 3450

$s = file_get_contents("http://pubapi.cryptsy.com/api.php?method=marketdatav2");
$s = json_decode($s);

print_r($s);

works like a charm

here is my curl version

$url = "http://pubapi.cryptsy.com/api.php?method=marketdatav2";
$ch=curl_init();
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$data=curl_exec($ch);
curl_close($ch);

$s = json_decode($data);
print_r($s);

Upvotes: 5

halfer
halfer

Reputation: 20487

It looks like that page is being served as windows-1252, but I believe JSON must be in UTF-8. Try converting it to UTF-8 first, and then run it through json_decode().

Upvotes: 1

Related Questions