javascript - 非标准json格式解析的问题
伊谢尔伦
伊谢尔伦 2017-04-11 11:08:31
0
5
809

1.

返回的是这个格式,PHP的json_decode();不能直接解析,难道要各种切割字符串成JSON格式,在用json_decode();解析?

这是标准的json格式.

2.

这个特别接近json格式了,但是还是无法解析.
各位有没有好的方法.

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(5)
黄舟

这里是 是不是 的问题
而不是 像不像 的问题

老老实实把提取出来吧。

伊谢尔伦
$res = file_get_contents('https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15011923452');
    /*"__GetZoneResult_ = {
    mts:'1501192',
    province:'广东',
    catName:'中国移动',
    telString:'15011923452',
    areaVid:'30517',
    ispVid:'3236139',
    carrier:'广东移动'
}"*/
    $res = trim(explode('=',$res)[1]);
    $res = iconv('gbk','utf-8', $res);
    $res = str_replace("'",'"', $res);
    $res = preg_replace('/(\w+):/is', '"$1":', $res);
    print_r(json_decode($res,1));
    /*Array
(
    [mts] => 1501192
    [province] => 广东
    [catName] => 中国移动
    [telString] => 15011923452
    [areaVid] => 30517
    [ispVid] => 3236139
    [carrier] => 广东移动
)*/
左手右手慢动作

PHP的话必须要提取成json字符串才行。。

迷茫

这是个js的属性对象吧...
如果不怕XSS注入的风险,可以直接扔到js里面。

迷茫

全部取出来=>用正则去掉第一个{前的内容=>JSON.parse

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!