Jack Marchetti
Jack Marchetti

Reputation: 15754

jQuery to parse JSON

I'm trying to parse the JSON returned from SocialMention.

Here's an example of what the JSON looks like:

{"title":"Social Mention","count":100,"items":[{"title":"RT @Jason_IIATMS: More Damon-isms that'll make you wanna puke: \"Let's hope the Chinese are right when they say this is the year of the tiger!\"","description":"","link":"http:\/\/twitter.com\/NYBD\/statuses\/9495530392","timestamp":1266876271,"image":null,"embed":null,"user":"NYBD","user_image":"http:\/\/a1.twimg.com\/profile_images\/60347208\/155135_logo_final_normal.jpg","user_link":"http:\/\/twitter.com\/NYBD","user_id":3265448,"source":"twitter","favicon":"http:\/\/twitter.com\/favicon.ico","type":"microblogs","domain":"twitter.com","id":"6111418866093918428"},

I'm using jquery's .getJson, so for instance:

$.getJSON("Home/GetSocialMentionData", function (data) {
    $.each(data.items, function (i, item) {
        alert(i);
    });
});

I'm obviously not doing something right because I never get to the alert(i) and frequently getting a JavaScript error "Microsoft JScript runtime error: 'length' is null or not an object"

I'm completely new to JSON, and I can't seem to find anything while googling for this.

So my question is, how do I parse the results? Any helpful advice would be great.

Upvotes: 2

Views: 6771

Answers (5)

Juraj Blahunka
Juraj Blahunka

Reputation: 18523

instead of $.getJSON, do classic AJAX call and specify type as JSON:

$.ajax({
    type: "GET",
    url: "Home/GetSocialMentionData",
    dataType: "json",
    success: function (data) {
        // parsed json
    }
})

EDIT

If the problem persists, I would reommend using the JSON parser and directly call var obj = JSON.parse(data) in your success function. If this fails, you have definitely problem with your json text

Upvotes: 3

Dave Ward
Dave Ward

Reputation: 60580

There's no need to manually deserialize the JSON. $.getJSON does that for you before the callback function is executed.

I'd suggest using Firebug (or comparable in-browser debugger) to set a breakpoint inside the callback, before the $.each(), and inspect what's actually being returned. It sounds like data.items doesn't exist or isn't an array.

Upvotes: 1

Dested
Dested

Reputation: 6433

This may not do anything, but does it really have the last "," at the end of your json string? That may infuriate the getJSON method...

EDIT: Upon further inspection, your JSON isnt valid. You can check it here http://json.parser.online.fr/

Edit: Alright, how about this

$.getJSON("Home/GetSocialMentionData", function(data) {
    for (var itemIndex in data.items) {
        var item = data.items[itemIndex];
        alert(item);
    }
});

It may just be

$.getJSON("Home/GetSocialMentionData", function(data) {
    for (var itemIndex in data) {
        var item = data[itemIndex];
        alert(item);
    }
});

Without seeing the rest of it its hard to say, but try that and see if you get an alert.

For future reference you can also use the JSON.stringify(string) method to figure out what a json string (or almost any object for that matter) contains. Try it out :-)

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630389

As of jQuery 1.4+ you JSON has to be valid to work, and I mean completely valid. You can validate your JSON using JSONLint here.

From what you posted, it's not valid...but appears to be a fragment, so enter your full result and see if you have any errors.

Upvotes: 5

James
James

Reputation: 12796

I have resolved these issues in the past by using the AJAX function http://api.jquery.com/jQuery.ajax/ of jQuery rather than getJSON.

Upvotes: 0

Related Questions