如何将 Google 电子表格的 Json 解码为 Php 数组

发布于 2024-08-17 15:49:52 字数 860 浏览 8 评论 0原文

我的 google 文档电子表格调用以 json 格式返回此响应
(我只需要“行”之后的所有内容) 请查看这里的格式化响应 :)

我使用php的json_decode 函数解析数据并使用它(是的,我对 php 很糟糕)此代码返回 NULL,并且根据文档,“如果 json 无法解码”,则返回 NULL

$json = file_get_contents($jsonurl);
$json_output = json_decode($json);

var_dump($json_output); // 返回 NULL

基本上,我想要完成的是从 Json 响应的第一行值创建一个简单的数组。

你们都是天才

$array = {'john','John Handcock','[email protected]','2929292','blanc'}

,我非常感谢你们的洞察力和帮助!

答案正如“sberry2A”提到的,响应不是有效的Json,谷歌提供了

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 protected]','2929292','blanc'}

You 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-08-24 15:49:52

您提供的链接中的数据不是有效的 JSON。您提供的似乎是解码版本。您可以看出这不是 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 protected]"},{"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 protected]"},{"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 protected] ) [3] => stdClass Object ( [v] => 2929292.0 [f] => 2929292 ) [4] => stdClass Object ( [v] => blanc ) ) ) ) 

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 protected]"},{"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 protected]"},{"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 protected] ) [3] => stdClass Object ( [v] => 2929292.0 [f] => 2929292 ) [4] => stdClass Object ( [v] => blanc ) ) ) ) 
热风软妹 2024-08-24 15:49:52

您是否尝试从响应 myData(...) 中删除回调函数?

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

风筝在阴天搁浅。 2024-08-24 15:49:52
$json = file_get_contents($jsonfile);
$data = json_decode($json);
print_r($data);
$json = file_get_contents($jsonfile);
$data = json_decode($json);
print_r($data);
别忘他 2024-08-24 15:49:52

PEAR 包 Services_Json 能够使用不带引号的键解析 JSON。因此,剥离回调并使用 Services_Json 进行解析,我相信这会起作用。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文