gruuuvy
gruuuvy

Reputation: 2129

php custom json_decode, some values are not enclosed by double quotations

Our server does not have the latest PHP versions, thus I cannot use json_decode. I tried to use the PEAR one: http://mike.teczno.com/JSON/JSON.phps, but the dataset is huge and it got the 30 seconds time out error - 30 seconds is unacceptable run time anyways.

I stumbled across this: http://www.php.net/manual/en/function.json-decode.php#108552 It almost works.

My JSON is in this form:

[{"number1":3,"number2":5,"time":"Jul 30, 2012 1:05:07 PM","value1":"aa","value2":"bb"}]

As you can see, the "number1" and "number2" fields have values that aren't enclosed by double quotations, which breaks the custom json_decode.

Does anyone know how to modify or provide a json_decode that can overcome this?

Upvotes: 0

Views: 349

Answers (2)

gruuuvy
gruuuvy

Reputation: 2129

As suggested in the comments, I used http://include-once.org/p/upgradephp which seems to work as a good json_decode() fallback.

Upvotes: 1

Johndave Decano
Johndave Decano

Reputation: 2113

Add in your PHP:

header("Content-type: application/json");

In the your javascript, use:

// Fetch the JSON text
var jsonFetch = $.ajax({
   url:"http://localhost/yourphp.php",
   async:false,
   cache:false,
}).responseText;
// Parse it so it will more formatted the right way
jsonData = JSON.parse(jsonFetch);

Upvotes: 0

Related Questions