两段采集来的JSON格式:更多API接口测试请点击注册
一:
{“hwgOnlineId”:“554312”, “jiwuChatId”:“”, “phoneCategoryId”:“20006”, “cuxiaoSeq”:{voucherTitle:1,lhvoucherTitle:2,isXYuanNItemTitle:3,taogouyhTitle:4,giftTitle:5,limitGifts:6,ordersGifts:7,jrPromTitle:8,purchaseTitle:9,couponTitle:10,newcouponTitle:11,yfbTitle:12,rxfTitle:13,scodeTitle:14,pointTitle:15,freightfreeTitle:16,govTitle:17,jnbtTitle:18}, “blackCategoryCode”:“R9010501”}
二:
{“pic”:“//imgservice.suning.cn/uimg1/b2c/image/rX4cM65dyr_4kndNFwLEgA.jpg_400w_400h_4e”, “itemDomain”:“//”+document.location.hostname, “resRoot”:“//script.suning.cn/project/pdsWeb”, “shopCount”:1, “sslStoreCode”:“”, “addCartNumLimit”:“199”, “favoriteStatusSwitch”:“1” == “1”, “thirdEVoucherFlag”:“”, “specialSSLFlag”:“”, “sslLabelText” : “苏宁服务”}
直接使用PHP的json_decode报错
解决思路:
1、手动格式化,将所有","换行
2、半分法删除内容定位报错的代码行
3、找到问题行后过滤格式处理
4、对于json格式中掺杂了javascript代码的情况还可以直接使用v8js引擎处理
解决方案一(修复格式):
$str = file_get_contents(‘test.txt’); KaTeX parse error: Undefined control sequence: \* at position 23: …reg_replace('@/\̲*̲[^/]+\*/@isU','…str); s t r = s t r r e p l a c e ( ′ " + d o c u m e n t . l o c a t i o n . h o s t n a m e ′ , ′ " ′ , str = str_replace('"+document.location.hostname','"',str=str r eplace( ′ "+document.location.hostname ′ , ′ " ′ ,str); s t r = s t r r e p l a c e ( ′ " 1 " = = " 1 " ′ , ′ t r u e ′ , str = str_replace('"1" == "1"','true',str=str r eplace( ′ "1"=="1" ′ , ′ true ′ ,str); j s o n = j s o n d e c o d e ( json = json_decode(json=json d ecode(str); if(!$json) j s o n = j s o n 2 a r r a y ( json = json2array(json=json2array(str); var_dump(getJsJSON($json));
解决方案二(使用PHP-V8JS扩展):
//以JavaScript环境执行JS function getJsJSON($str){ $v8 = new V8Js(); KaTeX parse error: Expected '}', got 'EOF' at end of input: …}} a={str} return JSON.stringify(a); } b = getJsJSON() "; try { //传递参数给js // $v8->str = $str; //执行js $code = v 8 − > e x e c u t e S t r i n g ( v8->executeString(v8−>executeString(func); //清空对象,垃圾回收。 unset($v8); } catch (V8JsScriptException KaTeX parse error: Expected '}', got 'EOF' at end of input: … dump(e->getMessage()); dump(e − > g e t J s S o u r c e L i n e ( ) ) ; d u m p ( e->getJsSourceLine()); dump(e−>getJsSourceLine());dump(e->getJsTrace()); // dump(KaTeX parse error: Expected 'EOF', got '}' at position 11: e); }̲ if(code)c o d e = j s o n d e c o d e ( code=json_decode(code=json d ecode(code,true); return $code; } j s = f i l e g e t c o n t e n t s ( ′ t e s t . t x t ′ ) ; v a r d u m p ( g e t J s J S O N ( js = file_get_contents('test.txt'); var_dump(getJsJSON(js=file g et c ontents( ′ test.txt ′ );var d ump(getJsJSON(js));