读取JSON问题

I am having a hard time reading the following JSON (basically no errors and nothing happens) and was hoping to get help identifying the issue with the function. I am unclear if the ajax is successful at grabbing it or if it's just not reading it correctly afterwards. Any help would be much appreciated!

Here is the structure of the JSON response:

   [{"attributes":{"type":"Account","url":"/services/data/v25.0/sobjects/Account/00130000015KJlWAAW"},"id":"00130000015KJlWAAW","locname":"Central Valley Ag","address":"607 North Robinson","city":"Hartington","state":"NE","postal":"68739","phone":"402-254-3354","web":"","lat":"42.627212","lng":"-97.269283"},{"attributes":{"type":"Account","url":"/services/data/v25.0/sobjects/Account/00130000015KJlXAAW"},"id":"00130000015KJlXAAW","locname":"Central Valley Ag","address":"709 Centennial Rd","city":"Wayne","state":"NE","postal":"68787","phone":"402-375-3510","web":"","lat":"42.235756","lng":"-96.998321"}]

This is the portion of the script which processes the JSON:

   function mapping(orig_lat, orig_lng, origin) {
                $(function () {
                    var dataType;
                    //jsonData is set to true
                    if (settings.jsonData == true) {
                        dataType = "jsonp";
                    } else {
                        dataType = "xml";
                    }
                    $.ajax({
                        type: "GET",
                        url: settings.dataLocation,
                        dataType: dataType,
                        crossDomain: true,                            
                        error: function(jqXHR, textStatus, errorThrown) {   
                    alert('Error Message: '+textStatus);
                    alert('HTTP Error: '+errorThrown);
                },
                        success: function (data) {
                            //After the store locations file has been read successfully
                            var i = 0;

                            if (settings.jsonData == true) {
                                var data = $.parseJSON(data);
                                //Process JSON
                                $.each(data, function () {
                                    var name = this.locname;
                                    var lat = this.lat;
                                    var lng = this.lng;
                                    var address = this.address;
                                    var address2 = this.address2;
                                    var city = this.city;
                                    var state = this.state;
                                    var postal = this.postal;
                                    var phone = this.phone;
                                    var web = this.web;
                                    web = web.replace("http://", "");
                                    var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);
                                    //Create the array
                                    locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);
                                    i++;
                                });
                            } else {
                                //Process XML
                                $(data).find('marker').each(function () {
                                    //Take the lat lng from the user, geocoded above
                                    var name = $(this).attr('name');
                                    var lat = $(this).attr('lat');
                                    var lng = $(this).attr('lng');
                                    var address = $(this).attr('address');
                                    var address2 = $(this).attr('address2');
                                    var city = $(this).attr('city');
                                    var state = $(this).attr('state');
                                    var postal = $(this).attr('postal');
                                    var phone = $(this).attr('phone');
                                    var web = $(this).attr('web');
                                    web = web.replace("http://", "");
                                    var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);
                                    //Create the array
                                    locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);
                                    i++;
                                });
                            }

Update: I added an ajax error handler as states and get the following parseerror when the page is loaded:

HTTP Error:Error: jQuery172036075869924388826_1343923258757 was not called

Then when I attempt to run the function I get the following error:

Uncaught TypeError: Property 'alert' of object [object Window] is not a function          dealer-locator-iframe2:273
$.fn.storeLocator.each.$.ajax.error dealer-locator-iframe2:273
f.Callbacks.o jquery.min.js:2
f.Callbacks.p.fireWith jquery.min.js:2
w jquery.min.js:4
f.ajaxTransport.send.d.onload.d.onreadystatechange

It looks like you need to run $.parseJSON on the data variable. data is coming back as a string of JSON and you need to convert it into a JavaScript object literal.

Example:

var obj = $.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

So place this immediately after if (settings.jsonData == true) in your success function:

var data = $.parseJSON(data);

The reason you're not getting any errors is because the $.each(function() {....}); isn't looping, because there isn't anything to loop on a string.

There's a really easy way to test for the data type of the data variable. Use this at the beginning of your success function:

alert(typeof(data));

If you get string returned, then you know that's the problem.

Before $.each add this line

data = eval("("+data+")");

and rest of your code will just work fine...

Why your code is not working? Reason - Because your are trying to run your loop on the string. First convert that string into the JSON object. eval() converts string to javascript object.

Your JSON looks like an array of objects, you can tell by those [] surrounding it. It is an array with just one element, but this gives errors if you write

json.key

because json is not an object but an array of objects, so json.key looks like you are calling some fncion "key" in the array which is not there. You should first select the real object itself with json[0] like

myJson = json[0]

an then

value = myJson.key1

if you're using some external API it is likely to be this issue as I faced this before