What is the most efficient apprach to read such JSON file
. To be honest what i need to take from that JSON
is the distance. Inside elements node
there could be either one or multiple items containing distance and duration
. I need to SUM
all of that values from each element
's node: distance-text
. For example: 324 km
(distance in kilemeters
). I shown below 3 examples. How can i do that?
Morover in last example if request is bad there could be something like "NOT_FOUND"
(or something else besides "OK"
). If in any "status"
there would be something diffrent than "OK"
at east in one i should get throw Exception()
.
JSON response example 1:
{
"destination_addresses" : [ "Długa 50, 05-075 Warszawa, Slovakia" ],
"origin_addresses" : [ "Wilenska 2, Zabrze, Slovakia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "324 km",
"value" : 323619
},
"duration" : {
"text" : "3 hours 16 mins",
"value" : 11776
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
In this case i should calculate as: 324
JSON response example 2: (There could be more elements like here)
{
"destination_addresses" : [ "Długa 50, 05-075 Warszawa, Slovakia" ],
"origin_addresses" : [ "Wilenska 2, Zabrze, Slovakia" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "324 km",
"value" : 323619
},
"duration" : {
"text" : "3 hours 16 mins",
"value" : 11776
},
"status" : "OK"
},
{
"distance" : {
"text" : "192 km",
"value" : 191950
},
"duration" : {
"text" : "2 hours 26 mins",
"value" : 8732
},
"status" : "OK"
},
]
}
],
"status" : "OK"
}
In this case i should calculate as: 516
JSON response example 3: (with some statuses diffrent than "OK")
{
"destination_addresses" : [ "", "", "" ],
"origin_addresses" : [ "" ],
"rows" : [
{
"elements" : [
{
"status" : "NOT_FOUND"
},
{
"status" : "OK"
},
{
"status" : "NOT_FOUND"
}
]
}
],
"status" : "ERR"
}
In this case i should get Exception