Parsing a JSON response
I have a web response that is JSON, how do I loop through it to create features or even just graphics layer, along with other info.
{
"formatVersion": "0.0.12",
"copyright": "Copyright 2017",
"routes": [
{
"summary": {
"lengthInMeters": 18558,
"travelTimeInSeconds": 1283,
},
"legs": [
{
"summary": {
"lengthInMeters": 2510,
"travelTimeInSeconds": 311,
},
"points": [
{
"latitude": -37.79351,
"longitude": 145.03255
},
{
"latitude": -37.79349,
"longitude": 145.03248
}
}
]
},
{
"summary": {
"lengthInMeters": 16048,
"travelTimeInSeconds": 973,
},
"points": [
{
"latitude": -37.81751,
"longitude": 145.15287
},
{
"latitude": -37.81743,
"longitude": 145.15159
},
{
"latitude": -37.81761,
"longitude": 145.15158
},
{
"latitude": -37.81872,
"longitude": 145.15137
},
{
"latitude": -37.81896,
"longitude": 145.15132
}
]
}
],
"sections": [
{
"startPointIndex": 0,
"endPointIndex": 297,
"sectionType": "TRAVEL_MODE",
"travelMode": "car"
}
]
}
]
}
-
I did originally ask this BEFORE I realised there was a solution to my XML query on the response from the same service
0 -
that doesn't mean I am not interested in the solution to the above question. I very much am
0 -
Hi Robert, just getting the chance to take a look at this now-
The above JSON wasn't valid (see https://jsonlint.com), so I tweaked it a bit. Let me know if this looks right to you?
{ "formatVersion": "0.0.12", "copyright": "Copyright 2017", "routes": [{ "summary": { "lengthInMeters": 18558, "travelTimeInSeconds": 1283 }, "legs": [{ "summary": { "lengthInMeters": 2510, "travelTimeInSeconds": 311 }, "points": [{ "latitude": -37.79351, "longitude": 145.03255 }, { "latitude": -37.79349, "longitude": 145.03248 } ] }, { "summary": { "lengthInMeters": 16048, "travelTimeInSeconds": 973 }, "points": [{ "latitude": -37.81751, "longitude": 145.15287 }, { "latitude": -37.81743, "longitude": 145.15159 }, { "latitude": -37.81761, "longitude": 145.15158 }, { "latitude": -37.81872, "longitude": 145.15137 }, { "latitude": -37.81896, "longitude": 145.15132 } ] } ], "sections": [{ "startPointIndex": 0, "endPointIndex": 297, "sectionType": "TRAVEL_MODE", "travelMode": "car" } ] } ] }Since we have JSON we can work with it as an object from the get-go, either from $webrequest1.json or $parseJson1.result, depending on where it comes from. Since it's not in the same structure as a featureset, we'll have to use it to build one.
Ideally, how would you want the results to be interpreted? A featureset of 7 points, two featuresets of 2/5 points, a featureset of 1 line, a featureset of 2 separate lines, etc? 2 lines makes the most sense if we needed to include the summary information as attributes, but I noticed that the second "leg" doesn't start exactly where the first one left off, so I'm not sure if that interpretation is correct.
0
Vous devez vous connecter pour laisser un commentaire.
Commentaires
3 commentaires