SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    {"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:
    Code:
    {"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.

    Code:
    <!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...jsoncallback=?) which has brackets, it works fine.

    Any help much appreciated
    Garrett

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,498
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    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
    HTML Code:
    function check()
    {
    
        $.getJSON("sitesjson.json", function(data)
        {
            alert(data);
        });
    
    }
    Does it say object, or better yet the data?

  3. #3
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    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).

  5. #5
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi ScallioXTX

    It returns application/x-javascript.

    Garrett

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    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...

  7. #7
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •