views:

44

answers:

2

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}); 
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);
A: 

Its not valid JSON. The keys should be wrapped inside quotes.

You can validate your json using the excellent JSON Lint site.

This is a valid version of the data returned:

 {
"items": [
    {
        "url": "http://fairfield.ebayclassifieds.com/",
        "name": "Fairfield"
    },
    {
        "url": "http://newyork.ebayclassifieds.com/",
        "name": "New York City"
    }
],
 "error": "null"
}
danp
I tried $test = '{items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null}'; $js = json_decode($test, true);echo " the js is $js"; and is not working either
Michael
how do you mean by "not working"? which is the error?
danp
Updated answer, its not valid, sorry :)
danp
+1  A: 

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 gives:

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

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).

Update:

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

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

This encloses the keys in quotes.


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

$a = json_decode($json, true);

which would give you:

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

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


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

Felix Kling
is there any way to make use of the current "invalid json" ? basically that's how I'm getting it from ebayclassifieds so I cannot change the way is displayed because I don't own ebayclassifieds site :)
Michael
@Michael : See my updated answer...
Felix Kling
@Felix Kling Now I have $test = '({items:[{url:"http://fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"http://newyork.ebayclassifieds.com/",name:"New York City"}],error:null});'; $test = trim(trim($test), "();"); $test = preg_replace('/(\w+):/i', '"\1":', $test); $js = json_decode($test, true);echo " the js is $js";but is not working :(
Michael
@Michael : Well, do `echo $test` after the replacement and look whether it is valid and check what `json_last_error()` gives you. BTW. `echo " the js is $js";` will not work anyway, because `$js` will contain an array, not a string. You have to extract the information like I showed you in my answer.
Felix Kling