1

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);
2
  • 2
    The problem is, that this is not valid JSON. The "keys" have also to be inside quotes (i.e. {"items":[{"url":"http://fairfield.ebayclassifieds.com/",name:"Fairfield"}... I played a little but the json_decode function generates a JSON_ERROR_SYNTAX error because of the faulty quotes. Commented Jun 19, 2010 at 18:29
  • Just realised this too, and updated answer to reflect it. Commented Jun 19, 2010 at 18:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 :)
Now I have $test = '({items:[{url:"fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"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 : 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.
0

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"
}

2 Comments

I tried $test = '{items:[{url:"fairfield.ebayclassifieds.com/",name:"Fairfield"},{url:"newyork.ebayclassifieds.com/",name:"New York City"}],error:null}'; $js = json_decode($test, true); echo " the js is $js"; and is not working either
Updated answer, its not valid, sorry :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.