tags:

views:

288

answers:

5

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.

+1  A: 

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

Dested
I didn't post the entire result because it's huge. I'm returning the JSON, i'm wondering how to parse it in the $.each loop
Jack Marchetti
nope, no alerts.
Jack Marchetti
Never use "for in" to iterate over arrays in JavaScript. Also, never use a "for in" loopt without using "hasOwnProperty".
BYK
+2  A: 

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

Juraj Blahunka
`getJSON` is a shortcut function which translates to exactly what you wrote http://api.jquery.com/jQuery.getJSON/
Marek Karbarz
It is, however as James pointed out, it sometimes solves stated problem
Juraj Blahunka
the states problem is how do I parse the JSON, which he put in as ://parsed json
Jack Marchetti
okay that works, but back to my stated problem, now that I have the obj variable. how do I then translate that, into HTML of <p>'s for instance.
Jack Marchetti
@Jack - Try using jQuery $.each method to iterate over each item in the array. Then you can use the .append method to add the p tags to your given parent element.
James
A: 

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

James
right, yeah, I'm asking how do I parse the JSON
Jack Marchetti
`getJson` is just a shortcut function to `$.ajax` call - so there's nothing different going on depending on which function is used
Marek Karbarz
@Jack - Why would you want to manually parse the JSON when you don't need to? The returned value from $.ajax is a fully eval'd object in which you can access methods and properties of your JSON data structure.
James
@James -- well that's what I don't know about then. I mean I just kind of want to show the top 3 returned results in an unordered list. I just havent found out how to do that.
Jack Marchetti
@Jack - Try the code in Juraj's comment above. you should be able to tell with firebug if the data variable contains something. Sometimes you have to use data.d to get to the data root depending on the shape of the returned JSON.
James
+2  A: 

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.

Nick Craver
+1 Not sure why this was downvoted, seems like a good suggestion to me.
Simon
+1  A: 

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.

Dave Ward