JQuery / JSON help

hi

Trying to parse json from a site and the site doesn’t have a lot of help yet for their new api so I’m a bit stuck. The json comes back from the site as follows.

If it’s successful:

{"result":[{"vid":"111111","title":"name","type":"archived","username":"myusername","size":"320x240","created":"1267818519","length":"0","upvotes":"0","preview":"","mirrors":"","views_live":0,"views_total":0,"comment_count":0,"page":"http:\\/\\/site.com\\/v\\/111111","deviceClass":"webcam"}]}

If it’s a fail:

{"error":"Could not find any matching videos."}

At the moment I’m working with the fail as it’s simpler so I have the following but my alert does nothing.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title></title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">

function check()
{

    $.getJSON("sitesjson.json", function(data)
    {
        alert(data.error);
    });

}

</script>

</head>

<body onload="check()">

</body>
</html>

Am I missing something here or is the JSON badly formed as it looks like it’s missing outer brackets? If I test on this flickr json (http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?) which has brackets, it works fine.

Any help much appreciated
Garrett

I’m not the best person to answer your question as I’ve only looked at jason a bit and jquery a bit more. But hopefully I’ll learn something from your thread.

I’m wondering if it’s a syntax thing. If you try

function check()
{

    $.getJSON("sitesjson.json", function(data)
    {
        alert(data);
    });

}

Does it say object, or better yet the data?

Hi

No it says “Null” but when I use the flickr json I get [object object] (what you’d expect to get) which is why I’m wondering is the json file malformed.

Garrett

What Content-Type does the webserver send for the JSON file?
You can check by inspecting the headers using the HTTP Live Headers plugin for Firefox. I can imagine the server sends something like text/plain. jQuery 1.4 is quite strict and doesn’t accept that AFAIK. It needs application/json I believe.
jQuery < 1.4 don’t really care all that much about the Content-Type I believe (not sure though).

hi ScallioXTX

It returns application/x-javascript.

Garrett

Can you get it to return application/json, or is that out of your hands?
Also, which version of jQuery are you using?
The comment I made about Content-Types doesn’t apply to jQuery versions lower than 1.4, and even for 1.4 I’m not sure if my comment is valid for $.getJSON, since the changelog only mentions that $.get() can now autosense JSON when the returned Content-Type is application/json, when no dataType is given. However, when using $.getJSON() a datatype is given …
I’ll try and look into this a little more tomorrow if I have the time…

hi

>Can you get it to return application/json, or is that out of your hands?

Third party api so no can’t do anything about it.

>which version of jQuery are you using?

jQuery JavaScript Library v1.4.2

I’ve tried $.get() as well as $.getJSON (not sure what the difference is) but it does not work for that either.

Garrett