Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php json解码-获取一个值_Php_Json - Fatal编程技术网

php json解码-获取一个值

php json解码-获取一个值,php,json,Php,Json,我试图从json内容中提取一个特定的值。这里是与json代码的链接,您可以看到显示的代码是 json({items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}); json({items:[{url:,name:“Fairfield”},{url:,name:

我试图从json内容中提取一个特定的值。这里是与json代码的链接,您可以看到显示的代码是 json({items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}); json({items:[{url:,name:“Fairfield”},{url:,name:“New York City”}),错误:null}); 我需要提取第一个url,在本例中是“”,它的名称值是“Fairfield”,我可以用正则表达式来提取,但我更喜欢使用json_解码。不幸的是,当我试图解码它不工作

$json = getContent("http://www.ebayclassifieds.com/m/AreaSearch?jsoncallback=json&lat=41.1131514&lng=-74.0437521"); $test = json_decode($json, true); $json=getContent(“http://www.ebayclassifieds.com/m/AreaSearch?jsoncallback=json&lat=41.1131514&lng=-74.0437521"); $test=json_decode($json,true);
它不是有效的JSON。键应该用引号括起来

您可以使用优秀的站点验证json

这是返回数据的有效版本:

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

正如danp已经说过的,返回的JSON包含在函数调用中(由
jsoncallback=JSON
指定)。您无法完全消除这种情况,但只需使用
AreaSearch?jsoncallback=&lat=41.1131514&lng=-74.0437521
至少删除字符串开头的
json
,您可以通过以下方式消除括号:

$json = trim(trim($json), "();");
提供:

{items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}
不幸的是,JSON字符串无效。键(
url
,…)必须用引号括起来。“。您可以轻松地检查是否出现语法错误(错误代码
4
JSON\u error\u syntax

更新:

根据以下问题:,您可以通过以下方式使JSON字符串有效:

$json = preg_replace('/(\w+):/i', '"\1":', $json);
这将键括在引号中


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

$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和名称



但我重复一遍,您作为响应得到的JSON无效,您无法使用原始形式的
JSON_decode()
对其进行解析。

我尝试了$test='{items:[{url:},{url:“York City”},错误:null};$js=JSON_decode($test,true);echo“js是$js”"; 更新后的答案无效,抱歉:)问题是,这不是有效的JSON。“键”也必须在引号内(即
{“items”:[{“url”:”http://fairfield.ebayclassifieds.com/,名称:“费尔菲尔德”}…
我玩了一点,但是
json\u decode
函数由于错误的引号生成了一个
json\u ERROR\u语法
错误。我也意识到了这一点,并更新了答案来反映它。有没有办法利用当前的“无效json”基本上,这就是我从ebayclassifieds获取它的方式,因此我无法更改显示方式,因为我没有自己的ebayclassifieds站点:)现在我有$test='({items:[{url:},{url:},{url:“York City”},error:null});;$test=trim(trim($test),“()”;”;$test=preg(replace('/(\w+)://I',“\1:”,$test);$js=json解码($test,true);echo“js是$js”;但不起作用:(@Michael:好吧,在替换后做
echo$test
,看看它是否有效,并检查
json\u last\u error()
给你什么。顺便说一句。
echo“js是$js”
无论如何都不会起作用,因为
$js
将包含一个数组,而不是字符串。您必须像我在回答中显示的那样提取信息。