如何将JS中的json对象转换成PHP可以识别的json对象

遇到个获取汇率的需求

找到和讯网的一个页面,http://forex.hexun.com/rmbhl/,看到页面上有个接口可以拿数据

拿到后发现是个js的json对象

$data = "PereMoreData([{currency:'阿尔及利亚第纳尔',refePrice:'5.805',code:'NGN '},{currency:'阿根廷比索',refePrice:'18.221',code:'ARS '},{currency:'阿联酋迪拉姆',refePrice:'186.634',code:'AED '},{currency:'阿曼里亚尔',refePrice:'1781.315',code:'OMR '},{currency:'埃及镑',refePrice:'38.293',code:'EGP '},{currency:'爱尔兰镑',refePrice:'470.421',code:'IEP '},{currency:'奥地利先令',refePrice:'57.168',code:'ATS '},{currency:'澳大利亚元',refePrice:'482.168',code:'AUD '},{currency:'澳门元',refePrice:'84.950',code:'MOP '},{currency:'巴基斯坦卢比',refePrice:'4.945',code:'PKR '},{currency:'巴拉圭瓜拉尼',refePrice:'0.115',code:'PYG '},{currency:'巴林第纳尔',refePrice:'1818.652',code:'BHD '},{currency:'巴拿马巴尔博亚',refePrice:'685.450',code:'PAB '},{currency:'巴西雷亚尔',refePrice:'176.644',code:'BRL '},{currency:'百慕大元',refePrice:'685.450',code:'BMD '},{currency:'保加利亚列弗',refePrice:'401.270',code:'BGN '},{currency:'比利时法郎',refePrice:'19.501',code:'BEF '},{currency:'冰岛克朗',refePrice:'5.904',code:'ISK '},{currency:'波兰兹罗提',refePrice:'183.315',code:'PLN '},{currency:'玻利维亚诺',refePrice:'99.920',code:'BOB '},{currency:'博茨瓦纳普拉',refePrice:'7354.614',code:'BWP '},{currency:'丹麦克朗',refePrice:'105.362',code:'DKK '},{currency:'德国马克',refePrice:'402.212',code:'DEM '},{currency:'法国法郎',refePrice:'119.922',code:'FRF '},{currency:'菲律宾比索',refePrice:'13.051',code:'PHP '},{currency:'芬兰马克',refePrice:'132.303',code:'FIM '},{currency:'港币',refePrice:'87.485',code:'HKD '},{currency:'哥伦比亚比索',refePrice:'0.211',code:'COP '},{currency:'古巴比索',refePrice:'685.450',code:'CUP '},{currency:'哈萨克坚戈',refePrice:'1.785',code:'KZT '},{currency:'韩元',refePrice:'0.613',code:'KRW '},{currency:'荷兰盾',refePrice:'356.968',code:'NLG '},{currency:'加拿大元',refePrice:'504.973',code:'CAD '},{currency:'加纳塞地',refePrice:'138.517',code:'GHC '},{currency:'捷克克朗',refePrice:'30.571',code:'CZK '},{currency:'津巴布韦元',refePrice:'2.135',code:'ZWD '},{currency:'卡塔尔里亚尔',refePrice:'188.295',code:'QAR '},{currency:'科威特第纳尔',refePrice:'2259.974',code:'KWD '},{currency:'克罗地亚库纳',refePrice:'106.229',code:'HRK '},{currency:'肯尼亚先令',refePrice:'6.737',code:'KES '},{currency:'拉脱维亚拉图',refePrice:'1349.842',code:'LVL '},{currency:'老挝基普',refePrice:'0.080',code:'LAK '},{currency:'黎巴嫩镑',refePrice:'0.455',code:'LBP '},{currency:'立陶宛立特',refePrice:'241.824',code:'LTL '},{currency:'林吉特',refePrice:'165.768',code:'MYR '},{currency:'卢布',refePrice:'9.861',code:'RUB '},{currency:'罗马尼亚列伊',refePrice:'169.205',code:'RON '},{currency:'毛里求斯卢比',refePrice:'20.790',code:'MUR '},{currency:'美元',refePrice:'685.450',code:'USD '},{currency:'蒙古图格里克',refePrice:'0.261',code:'MNT '},{currency:'孟加拉塔卡',refePrice:'8.171',code:'BDT '},{currency:'秘鲁新索尔',refePrice:'203.700',code:'PEN '},{currency:'缅甸缅元',refePrice:'0.445',code:'BUK '},{currency:'摩洛哥迪拉姆',refePrice:'71.816',code:'MAD '},{currency:'墨西哥比索',refePrice:'34.918',code:'MXN '},{currency:'南非兰特',refePrice:'47.824',code:'ZAR '},{currency:'挪威克朗',refePrice:'79.248',code:'NOK '},{currency:'欧元',refePrice:'786.608',code:'EUR '},{currency:'葡萄牙埃斯库多',refePrice:'3.924',code:'PTE '},{currency:'人民币',refePrice:'100.000',code:'CNH '},{currency:'日元',refePrice:'6.272',code:'JPY '},{currency:'瑞典克朗',refePrice:'77.117',code:'SEK '},{currency:'瑞士法郎',refePrice:'698.370',code:'CHF '},{currency:'沙特里亚尔',refePrice:'182.743',code:'SAR '},{currency:'斯里兰卡卢比',refePrice:'3.750',code:'LKR '},{currency:'索马里先令',refePrice:'1.203',code:'SOS '},{currency:'泰国铢',refePrice:'21.235',code:'THB '},{currency:'坦桑尼亚先令',refePrice:'0.299',code:'TZS '},{currency:'突尼斯第纳尔',refePrice:'229.232',code:'TND '},{currency:'土耳其里拉',refePrice:'129.294',code:'TRY '},{currency:'危地马拉格查尔',refePrice:'88.605',code:'GTQ '},{currency:'委内瑞拉博利瓦',refePrice:'0.003',code:'VEB '},{currency:'乌拉圭比索',refePrice:'21.159',code:'UYU '},{currency:'西班牙比塞塔',refePrice:'4.728',code:'ESP '},{currency:'希腊德拉克马',refePrice:'2.309',code:'GRD '},{currency:'新加坡元',refePrice:'502.787',code:'SGD '},{currency:'新台币',refePrice:'22.263',code:'TWD '},{currency:'新西兰元',refePrice:'460.281',code:'NZD '},{currency:'匈牙利福林',refePrice:'2.450',code:'HUF '},{currency:'牙买加元',refePrice:'5.445',code:'JMD '},{currency:'以色列谢克尔',refePrice:'183.021',code:'ILS '},{currency:'意大利里拉',refePrice:'0.406',code:'ITL '},{currency:'印度卢比',refePrice:'9.802',code:'INR '},{currency:'印尼盾',refePrice:'0.047',code:'IDR '},{currency:'英镑',refePrice:'874.075',code:'GBP '},{currency:'约旦第纳尔',refePrice:'966.784',code:'JOD '},{currency:'越南盾',refePrice:'0.030',code:'VND '},{currency:'智利比索',refePrice:'0.989',code:'CLP '}])";

如何转成php能识别的json对象

参考这个帖子,https://segmentfault.com/q/1010000008553610


$encode = mb_detect_encoding($data, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
$data = mb_convert_encoding($data, 'UTF-8', $encode);
$data = str_replace('PereMoreData([','',$data);
$data = str_replace('])','',$data);
$data = '[' . $data . ']';
$str = preg_replace(["/([a-zA-Z_]+[a-zA-Z0-9_]*)\s*:/", "/:\s*'(.*?)'/"], ['"\1":', ': "\1"'], $data);

这就可以转成php能识别的json对象了,最主要的就是这个正则替换

发布了14 篇原创文章 · 获赞 3 · 访问量 4405

猜你喜欢

转载自blog.csdn.net/kina100/article/details/85622312