开发者

How to decode Google spreadsheet's Json respose as a Php Array

开发者 https://www.devze.com 2022-12-16 15:00 出处:网络
My google Docs Spreadsheet call returns this response in the json format (I only need everything after \"rows\")

My google Docs Spreadsheet call returns this response in the json format

(I only need everything after "rows") please look at the formatted response here : )

I use php's json_decode function to parse the data and use it (Yes, I am awful at php) This code returns NULL, and according to the documentation, NULL is returned "if the json cannot be decoded".

$json = file_get_contents($jsonurl);

$json_output = json_decode($json);

var_dump ($json_output); // Returns NULL

Basically, what i want to accomplish is to make a simple array from the first row values of the Json response.

like this

$array = {'john','John Handcock','email@yahoo.com','2929292','blanc'}

Yo开发者_高级运维u guys are genius, I would appreciate your insight and help on this very much!

Answer as "sberry2A" mentions bellow, the response is not valid Json, google offers the Zend Json library for this purpose, tho I decided to parse the tsv-excel version instead :)


The data in the link you provided is not valid JSON. What you have provided appears to be the decoded version. You can tell that is in not JSON because the array keys are not quoted. For instance, version should be 'version'.

Your data should look more like this

'{"version":"0.6","reqId":"requestIDnumber","status":"ok","sig":"65724392","table":{"cols":[{"id":"A","label":"slug","type":"string", "pattern":""},{"id":"B","label":"name","type":"string","pattern":""},{"id":"C","label":"email","type":"string","pattern":""},{"id":"D","label" :"nsid","type":"number","pattern":"#0.###############"},{"id":"E","label":"theme","type":"string","pattern":""}],"rows":[{"c":[{"v":"mo"},{"v": "Mohammad Taheri"},{"v":"email@yahoo.com"},{"v":"2929292.0","f":"2929292"},{"v":"blanc"}]}]}}'



$json = '{"version":"0.6","reqId":"requestIDnumber","status":"ok","sig":"65724392","table":{"cols":[{"id":"A","label":"slug","type":"string", "pattern":""},{"id":"B","label":"name","type":"string","pattern":""},{"id":"C","label":"email","type":"string","pattern":""},{"id":"D","label" :"nsid","type":"number","pattern":"#0.###############"},{"id":"E","label":"theme","type":"string","pattern":""}],"rows":[{"c":[{"v":"mo"},{"v": "Mohammad Taheri"},{"v":"email@yahoo.com"},{"v":"2929292.0","f":"2929292"},{"v":"blanc"}]}]}}';
$data = json_decode($json);
print_r($data->table->rows);

//output

Array ( [0] => stdClass Object ( [c] => Array ( [0] => stdClass Object ( [v] => mo ) [1] => stdClass Object ( [v] => Mohammad Taheri ) [2] => stdClass Object ( [v] => email@yahoo.com ) [3] => stdClass Object ( [v] => 2929292.0 [f] => 2929292 ) [4] => stdClass Object ( [v] => blanc ) ) ) ) 


Did you try removing the callback function from the response myData(...)?


$json = file_get_contents($jsonfile);
$data = json_decode($json);
print_r($data);


The PEAR package Services_Json is able to parse JSON with unquoted keys. So, strip the callback and parse with Services_Json and I believe that will work.

http://mike.teczno.com/JSON/doc/

0

精彩评论

暂无评论...
验证码 换一张
取 消