我的 Ajax 呼叫如下
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);
...
}
我的控制臺日志回傳以下 Json:
{“一般”:{“id”:45423920,“時間戳”:“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":空},“空間”:{“緯度”:-23.7830482,“lng”:133.877221,“軸承”:165,“speed_can_kmh”:82,“speed_sat_kmh”:81,“vert_velocity”:0},“line_data”:{“line_0_type_l”:“實心”、“line_0_type_r”:“虛線”、“line_0_qual_l”:“優秀”、“line_0_qual_r”:“高”、“line_0_width_l”:0.13、“line_0_width_r”:0.16、“line_1_type_l”:“實心”、“line_1_type_r”:“實心”、“line_1_width_l”:0.14、“line_1_width_r”:0.21、“line_2_type_l”:“虛線”、“line_2_type_r”:“虛線”、“line_2_width_l”:0.00、“line_2_width_r”:0.00 ,“ldw_left_on”:假,“ldw_right_on”:假,“ldw_off”:假,“ldw_left_avail”:真,“ldw_right_avail”:真},“sign_data”:{“sign_type_1”:空,“sign_type_2”:空,“sign_type_3":空,"sign_type_4":空,"sign_type_5":空,"sign_type_6":空,"sign_type_7":空},"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”: "點","坐標":[133.877221,-23.7830482]},"屬性":{"id":45423920}}}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 ":"點","坐標":[133.877221,-23.7830482]},"屬性":{"id":45423920}}}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 ":"點","坐標":[133.877221,-23.7830482]},"屬性":{"id":45423920}}}
但是當我嘗試訪問 objParsed.general 時,它是未定義的 ,我想讀取 img_path。
我錯過了什么?
uj5u.com熱心網友回復:
也許你可以直接得到它:
function LoadPointDetails(obj) {
var img = obj.general.img_path;
alert(img);
}
在我的測驗中,我有一個 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);
}
然后我的 Ajax 呼叫:
$("#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.");
}
});
})

uj5u.com熱心網友回復:
我在這里找到了答案
我的 JSON 是雙重編碼的:
function LoadPointDetails(obj) {
currPointDet = obj;
var objParsed = JSON.parse(obj.trim());
var obj2 = JSON.parse(objParsed);
console.log(obj2.general);
}
這行得通!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491871.html
標籤:阿贾克斯 asp.net 核心 解析 序列化 json解析器
上一篇:PHP決議帶有節點子節點的xml
