PHP JSON解码-获取值 [英] php json decode - get a value

查看:94
本文介绍了PHP JSON解码-获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从json内容中提取特定值.这是与json代码 http: //www.ebayclassifieds.com/m/AreaSearch?jsoncallback=json&lat=41.1131514&lng=-74.0437521 您可能会看到显示的代码是

I'm trying to extract a specific value from json content . Here it is link with the json code http://www.ebayclassifieds.com/m/AreaSearch?jsoncallback=json&lat=41.1131514&lng=-74.0437521 As you may see the the code displayed is


json({items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}); 

我需要提取第一个网址,在这种情况下为" http://fairfield.ebayclassifieds.com/ ,其名称值为" Fairfield,我可以使用regex来实现,但我更喜欢使用json_decode.不幸的是,当我尝试解码时不起作用

I need to extract the first url which in this case is "http://fairfield.ebayclassifieds.com/" and its name value which is "Fairfield" , I could do it with regex but I would prefer to use json_decode. Unfortunately when I try to decode it doesn't work


$json  = getContent("http://www.ebayclassifieds.com/m/AreaSearch?jsoncallback=json&lat=41.1131514&lng=-74.0437521");
$test = json_decode($json, true);

推荐答案

如danp所述,返回的JSON包含在函数调用中(由jsoncallback=json指定).您不能完全摆脱这种情况,但是,仅使用AreaSearch?jsoncallback=&lat=41.1131514&lng=-74.0437521至少会删除字符串开头的json,并且可以通过以下方式消除括号:

As danp already said, the returned JSON is enclosed in a function call (specified by jsoncallback=json). You cannot get rid of this totally but, just using AreaSearch?jsoncallback=&lat=41.1131514&lng=-74.0437521 removes at least the json at the beginning of the string and you can get rid of the brackets by:

$json = trim(trim($json), "();");

with提供:

{items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}

不幸的是,JSON字符串无效.键(itemsurl,...)必须用引号"括起来.您可以使用 json_last_error()轻松检查是否收到语法错误. (错误代码4JSON_ERROR_SYNTAX).

Unfortunately, the JSON string is not valid. The keys (items, url, ...) have to be enclosed in quotes ". You can easily check that you get a syntax error with json_last_error() (error code 4, JSON_ERROR_SYNTAX).

更新:

根据此问题: 使用PHP进行的无效JSON解析 ,您可以使用以下命令使JSON字符串有效:

According to this question: Invalid JSON parsing using PHP , you can make the JSON string valid with:

$json = preg_replace('/(\w+):/i', '"\1":', $json);

这会将键括在引号中.

如果字符串有效,则可以通过以下方式生成数组:

If the string would be valid, then you could generate an array via:

$a = json_decode($json, true);

这将为您提供

Array
(
    [items] => Array
        (
            [0] => Array
                (
                    [url] => http://fairfield.ebayclassifieds.com/
                    [name] => Fairfield
                )
            [1] => Array
                (
                    [url] => http://newyork.ebayclassifieds.com/
                    [name] => New York City
                )
        )
    [error] => 
)

因此您可以通过$a['items'][0]['url']$a['items'][0]['name']分别获取第一个URL和名称.

So you could get the first URL and name via $a['items'][0]['url'] and $a['items'][0]['name'] resp.

但是我再说一遍,您作为响应得到的JSON是无效,并且您不能使用json_decode()原始格式对其进行解析.

But I repeat, the JSON you get as response is not valid and you cannot parse it with json_decode() in its original form.

这篇关于PHP JSON解码-获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆