如何使用純 javascript 讀取 JSON 陣列?
我需要讀取的陣列示例:
{
"GetRouteSummaryForStopResult": {
"StopNo": "1234",
"Error": "",
"StopDescription": "STREET 1 / STREET 2",
"Routes": {
"Route": {
"RouteNo": "1234",
"RouteHeading": "END_POINT",
"DirectionID": 1,
"Direction": "",
"Trips": {
"Trip": [
{
"Longitude": "-75",
"Latitude": "45",
"GPSSpeed": "",
"TripDestination": "END_POINT",
"TripStartTime": "10:30",
"AdjustedScheduleTime": "9",
"AdjustmentAge": "0.48",
"LastTripOfSchedule": false,
"BusType": "NFIN"
},
{
"Longitude": "",
"Latitude": "",
"GPSSpeed": "",
"TripDestination": "END_POINT",
"TripStartTime": "11:00",
"AdjustedScheduleTime": "29",
"AdjustmentAge": "-1",
"LastTripOfSchedule": false,
"BusType": ""
}
]
}
}
}
}
}
我正在嘗試在地圖中閱讀Longitude和Latitude放置。
我該怎么做呢?它是從外部 API 中提取的
我試過了:
function on_page_ld() {
var parse_data = "api.example.com/api_name/?key=abcdefg12345"
var jsonData = JSON.parse();
for (var i = 1; i < jsonData.Routes.Route.RouteNo; i ) {
var array = jsonData.Routes.Route.RouteNo[i];
console.log(array);
}
}
我得到:
VM4059:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
uj5u.com熱心網友回復:
async function on_page_ld(){
// Fetching data from API
const response = await fetch("api.example.com/api_name/?key=abcdefg12345")
// Parsing JSON
const data = await response.json()
// Your array of latitudes and longitudes
const locations = data.GetRouteSummaryForStopResult.Routes.Route.Trips.Trip
// get all your latitudes and longitudes
for(i = 0; i < locations.length; i ){
console.log(i ") latitude :" locations[i].latitude);
console.log(i ") longitude :" locations[i].longitude);
}
}
uj5u.com熱心網友回復:
您可以使用 for-in 回圈讀取值
for (i in locations.length) {
console.log(i ") latitude :" locations[i].latitude);
console.log(i ") longitude :" locations[i].longitude);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363227.html
標籤:javascript 数组 json
