views:

229

answers:

3

I'm using FQL to retrieve a list of users from Facebook. For consistency I get the result as JSON. This causes a problem - since the returned JSON encodes the user IDs as numbers, json_decode() converts these numbers to floating point values, because some are too big to fit in an int; of course, I need these IDs as strings.

Since json_decode() does its own thing without accepting any behavior flags, I'm at a loss. Any suggestions on how to resolve this?

A: 

Maybe write your own JSON parser? The syntax is not that hard, so it's probably easy to implement as a recursive function.

Emil Vikström
That's my last resort solution... I already wrote the Flash-side one so it's no big deal, but I can't believe there isn't a better solution.
ggambett
+1  A: 

Quick and dirty, seems to work for now :

$sJSON = preg_replace('/:(\d+)/', ':"${1}"', $sJSON);
ggambett
+3  A: 

json_decode() can convert large integers to strings, if you specify a flag in the function call:

$array = json_decode($json, true, 512, JSON_BIGINT_AS_STRING)
Björn
No idea how could I miss that in the documentation. Thanks!
ggambett