how to extract information from JSON using jQuery

Posted by Richard Reddy on Stack Overflow See other posts from Stack Overflow or by Richard Reddy
Published on 2010-04-19T17:35:31Z Indexed on 2010/04/19 17:43 UTC
Read the original article Hit count: 218

Filed under:
|
|
|

Hi,

I have a JSON response that is formatted from my C# WebMethod using the JavascriptSerializer Class. I currently get the following JSON back from my query:

{"d":"[{\"Lat\":\"51.85036\",\"Long\":\"-8.48901\"},{\"Lat\":\"51.89857\",\"Long\":\"-8.47229\"}]"}

I'm having an issue with my code below that I'm hoping someone might be able to shed some light on. I can't seem to get at the information out of the values returned to me. Ideally I would like to be able to read in the Lat and Long values for each row returned to me.

Below is what I currently have:

$.ajax({
                    type: "POST",
                    url: "page.aspx/LoadWayPoints",
                    data: "{'args': '" + $('numJourneys').val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d != '[]') {
                            var lat = "";
                            var long = "";
                            $.each(msg.d, function () {
                                lat = this['MapLatPosition'];
                                long = this['MapLongPosition'];
                            });
                            alert('lat =' + lat + ', long =' + long);
                        }
                    }
                });

I think the issue is something to do with how the JSON is formatted but I might be incorrect. Any help would be great.

Thanks, Rich

© Stack Overflow or respective owner

Related posts about JSON

Related posts about jQuery