Home > Enterprise >  nested object is undefined - Ajax call Response - Json Parser
nested object is undefined - Ajax call Response - Json Parser

Time:06-08

My Ajax call is as follows

function onMapClick(e) {

$.ajax({
    type: "GET",
    url: GET_MAP_PT_INFO_URI,
    data: { "lat": e.latlng.lat, "lng": e.latlng.lng },
    dataType: 'json',
    success: function (d) {
        LoadPointDetails(d);
        
    },
    error: function () {
        if (debugState) console.log("Error in call for chart data.");
    }
});

}


function LoadPointDetails(obj) {

currPointDet = obj;
var objParsed = JSON.parse(obj);

console.log(objParsed);
...
}

My console log returns the following Json:

{"general" : {"id" : 45423920, "timestamp" : "2019-06-30T23:41:38.2", "run_id" : 29211, "track_id" : 10317, "link_id" : 983544872, "dir" : "T", "vehicle_name" : "Transporter", "prev_id" : 45423919, "next_id" : 45423921, "track_start" : "2019-06-30T23:31:12.7", "track_end" : "2019-07-01T00:34:15.6", "img_path" : "https://xyz-abc.s3-ap-southeast-2.amazonaws.com/somecode/1/123444.jpg", "notes" : null, "flag" : null}, "spatial" : {"lat" : -23.7830482, "lng" : 133.877221, "bearing" : 165, "speed_can_kmh" : 82, "speed_sat_kmh" : 81, "vert_velocity" : 0}, "line_data" : {"line_0_type_l" : "solid", "line_0_type_r" : "dashed", "line_0_qual_l" : "excellent", "line_0_qual_r" : "high", "line_0_width_l" : 0.13, "line_0_width_r" : 0.16, "line_1_type_l" : "solid", "line_1_type_r" : "solid", "line_1_width_l" : 0.14, "line_1_width_r" : 0.21, "line_2_type_l" : "dashed", "line_2_type_r" : "dashed", "line_2_width_l" : 0.00, "line_2_width_r" : 0.00, "ldw_left_on" : false, "ldw_right_on" : false, "ldw_off" : false, "ldw_left_avail" : true, "ldw_right_avail" : true}, "sign_data" : {"sign_type_1" : null, "sign_type_2" : null, "sign_type_3" : null, "sign_type_4" : null, "sign_type_5" : null, "sign_type_6" : null, "sign_type_7" : null}, "previous_sign_data" : {"sign_type_1" : 9, "sign_type_2" : 9, "sign_type_3" : 0, "sign_type_4" : 0, "sign_type_5" : 0, "sign_type_6" : 0, "sign_type_7" : 0}, "GeoJson" : {"type":"Feature","geometry":{"type":"Point","coordinates":[133.877221,-23.7830482]},"properties":{"id":45423920}}}

but when i try to access objParsed.general it is undefined I want to read img_path.

What am I missing?

CodePudding user response:

maybe you can get it directly:

function LoadPointDetails(obj) {
    var img = obj.general.img_path;
    alert(img);
}

In my test, I have an api:

public IActionResult getData()
{
    var b = new { id = "45423920", img_path = "https://xyz-abc.s3-ap-southeast-2.amazonaws.com/somecode/1/123444.jpg" };
    var c = new { lat = -23.7830482, lng = 133.877221 };
    var a = new { general = b, spatial = c };
    return Json(a);
}

Then my Ajax call:

$("#btn3").click(function() {
    $.ajax({
        type: "GET",
        url: 'https://localhost:7151/home/getData',
        data: { "lat": 111, "lng": 222 },
        dataType: 'json',
        success: function (d) {
            LoadPointDetails(d);
        },
        error: function () {
            if (debugState) console.log("Error in call for chart data.");
        }
    });
})

enter image description here

CodePudding user response:

I found my answer here

My JSon was double encoded:

function LoadPointDetails(obj) {

currPointDet = obj;
var objParsed = JSON.parse(obj.trim());
var obj2 = JSON.parse(objParsed);

console.log(obj2.general);
}

This worked!

  • Related