【问题标题】:parsing text / json with php用 php 解析文本/json
【发布时间】:2012-09-01 15:06:45
【问题描述】:
{"email":"test@example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}

我无法解析这个字符串。它来自 Sendgrid 的事件 API,似乎“几乎”是 JSON,但 json_decode 不起作用。我的目标是将数据放入数组中,然后放入 MySQL 表中。我不是要求任何人为我编写代码,只是为我指出正确的方法。我是否使用爆炸功能,然后json_decode?。 (我正在慢慢自学PHP,如果问题不清楚,请见谅)

【问题讨论】:

  • 为什么说“几乎”?根据 JSONlint,这似乎是有效的。也许您可能想分享一些代码或其他详细信息?
  • 对我来说它看起来像 JSON - 您是否尝试过将结果插入一些在线 json 验证器来确定?如果没有返回正确的 json,可能是他们的错误。
  • PHP decoding json 的可能重复项
  • 感谢分享 JSONlint,我会回去编码,看看我能不能弄明白,我能够在一个短的 json 字符串上成功使用 json_decode 但不是上面的(它给了我 NULL) ,我会再试一次。再次感谢。

标签: php json parsing


【解决方案1】:

我认为你没有使用 $assoc = true 的 json_decode 函数,所以你得到的是一个对象而不是一个数组

$json = '{"email":"test@example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"}, "category":["EST_TEST","Newsletter"],"event":"open"}';

var_dump(json_decode($json, true));

你会得到数组的结果;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2013-12-10
    相关资源
    最近更新 更多