简体   繁体   中英

Can't parse JSON formatted response from server

I'm trying to parse a JSON formatted string like

var json_response = JSON.parse(response); 

response is as below. It's a formatted JSON response from a Google api.

{
 "location": {
  "lat": 12.9621211,
  "lng": 77.64804099999999
 },
 "accuracy": 740.0
}

However I get an error saying

Uncaught SyntaxError: Unexpected token {

I checked out a lot of answers in SO. A lot of them say the above json response is already an object. But when i tried

console.log(response["location"]);
console.log(response.location);

I get the following output

undefined
undefined

What am I doing wrong ?

EDIT:

console.log(response);

gives

{
 "location": {
  "lat": 12.9621211,
  "lng": 77.64804099999999
 },
 "accuracy": 740.0
}

UPDATE:

When i tried the following

console.log('"'+response+'"');

I get

"{
 "location": {
  "lat": 12.962118199999999,
  "lng": 77.6480399
 },
 "accuracy": 739.0
}

"

Seems like there is an extra line after the closing }. Will that make any difference ?

I'm pasting my whole function here. sorry if i was not clear before.

    function get_distance_from_cellTower(json){
        $.ajax({type: 'POST', url:"get_location.php",data:getformurlencoded(json),
            success:function(response){
                console.log('"'+response+'"');
                var latitude;
                var longitude;
                var success;
                var json_response;
                try{
                    json_response = JSON.parse(response);
                    if(json_response.hasOwnProperty("error")){
                        success = 0;
                        console.log(json_response.error);
                        append_to_show(json_response.error);
                    }
                    if(json_response.hasOwnProperty("location")){
                        success = 1;
                    }
                }
                catch(e){
                    console.log(e);
                    append_to_show(e);
                }

                if(success){
                    var location = JSON.parse(json_response.location);
                    latitude = parseFloat(location.lat);
                    longitude = parseFloat(location.lng);
                    var distance = calculate_distance_kms(latitude, doclat, longitude, doclong);
                    append_to_show("cell tower: "+distance);
                    console.log("Cell tower: "+distance);
                }
            },

            error:function(err){
                console.log(err);
                append_to_show(err);
            },contentType:'application/x-www-form-urlencoded'});

    }  

The problem is that you are trying to parse the next level also. When you parse from JSON to an object, it will parse all levels. You get an object containing objects, not an object containing JSON strings that needs to be parsed.

Just get the object from the property:

var location = json_response.location;

When the JSON is parsed, the values are converted to the proper data type, so you don't need to parse them:

latitude = location.lat;
longitude = location.lng;

If the JSON had contained string values instead of number values for the lat and lng properties, you would have needed to parse them:

{
 "location": {
  "lat": "12.9621211",
  "lng": "77.64804099999999"
 },
 "accuracy": 740.0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM