如何在php中修复格式错误的JSON [英] how to fix a malformed JSON in php
问题描述
我有这个JSON字符串,我想使用json_decode();
函数对其进行解码
i have this JSON string that i want to decode it with json_decode();
function
{"phase":2,"id":"pagelet_profile_picture","css":["VCxcl","Ix2pq"],"js":["fZYUE","VfnZ3"],"content":{"pagelet_profile_picture":"\u003cdiv class=\"profile-picture\">\u003cspan class=\"profile-picture-overlay\">\u003c\/span>\u003cimg class=\"photo img\" src\=\"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-snc4\/222_111_2222_n.jpg\" alt=\"bla bla\" id=\"profile_pic\" \/>\u003c\/div>"}}
有json_last_error();
,但对我没有帮助. (有时会获取JSON_ERROR_STATE_MISMATCH和JSON_ERROR_SYNTAX)
there is the json_last_error();
but it not helping me. (got JSON_ERROR_STATE_MISMATCH and JSON_ERROR_SYNTAX sometimes)
我想知道此JSON字符串出了什么问题以及如何在PHP中自动修复它,以便可以对其进行解码. 一些代码将非常有帮助 谢谢.
i want to know what wrong with this JSON string and how i can fix it automatically in PHP so i can decode it. some code will be very helpful thanks.
推荐答案
使用json lint,看来问题出在src \ =
Using a json lint, it seems the problem is the src\=
\转义=符号,这没有任何意义.
the \ escapes the = sign, which makes no sense.
如果将src \ =替换为src =,它将通过验证程序.
If you replace src\= with src= it passes the validator.
解决方法:
- 首先修复生成json字符串的代码.
或
- 使用 str_replace 将'src \ ='更改为'src ='
- use str_replace to change 'src\=' to 'src='
这篇关于如何在php中修复格式错误的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!