PHP json_decode在无效的JSON上返回NULL

时间:2018-03-19 17:28:20

标签: php json

以下更新的解决方案

这是假设的json:

a: 14: {
s: 9: "rating_id";s: 24: "rwp_rating_5aafe3e80b03d";s: 14: "rating_post_id";i: 3293;s: 16: "rating_review_id";s: 32: "14855d71ef49debd6f9493c13b1bf84b";s: 12: "rating_score";a: 1: {
    i: 0;d: 5;
}
s: 14: "rating_user_id";i: 2;s: 16: "rating_user_name";s: 0: "";s: 17: "rating_user_email";s: 0: "";s: 12: "rating_title";s: 0: "";s: 14: "rating_comment";s: 11: "dsfusof9832";s: 13: "rating_images";a: 0: {}
s: 11: "rating_date";i: 1521476584;s: 13: "rating_status";s: 7: "pending";s: 15: "rating_verified";b: 0;s: 15: "rating_template";s: 26: "rwp_template_5a1d7fcb9457e";
}

但是,当我使用JSONLint检查它时,它不会验证。我收到错误:

错误:第1行的解析错误: a:14:{s:9:"老鼠 ^ 期待' STRING',' NUMBER',' NULL'' TRUE',' FALSE',' {',' [',得到' undefined'

我需要提取" rating_comment"

的值

有人可以帮助制作这个有效的JSON或帮助提取" rating_comment"的价值?

我不知道这是序列化字符串而不是JSON。谢谢你指出这一点。我将使用unserialize来获取我需要的数据。 :)

1 个答案:

答案 0 :(得分:2)

您必须使用unserialize()来获取价值。但是您的字符串不是有效的序列化字符串。您必须删除多余的空格和换行符(不能像JSON那样更改字符串)。

$serial='a:14:{s:9:"rating_id";s:24:"rwp_rating_5aafe3e80b03d";s:14:"rating_post_id";i:3293;s:16:"rating_review_id";s:32:"14855d71ef49debd6f9493c13b1bf84b";s:12:"rating_score";a:1:{i:0;d:5;}s:14:"rating_user_id";i:2;s:16:"rating_user_name";s:0:"";s:17:"rating_user_email";s:0:"";s:12:"rating_title";s:0:"";s:14:"rating_comment";s:11:"dsfusof9832";s:13:"rating_images";a:0:{}s:11:"rating_date";i:1521476584;s:13:"rating_status";s:7:"pending";s:15:"rating_verified";b:0;s:15:"rating_template";s:26:"rwp_template_5a1d7fcb9457e";}';

$obj=unserialize($serial);
echo $obj['rating_comment'];

输出:

dsfusof9832