I receive a String
of Lats & Longs from an API call in Json
"lineStrings": [
"[[[-0.301457,51.515017],[-0.288265,51.51014],[-0.280462,51.503057],[-0.267972,51.494627],[-0.254555,51.495148],[-0.245704,51.494917],[-0.235881,51.494122],[-0.22362,51.4923],[-0.213427,51.490311],[-0.206636,51.490459],[-0.193378,51.492063],[-0.182658,51.494316],[-0.174138,51.494094],[-0.156377,51.49227],[-0.143102,51.496359],[-0.133608,51.499544],[-0.124861,51.50132],[-0.122666,51.507058],[-0.11426,51.511006],[-0.103659,51.511581],[-0.094009,51.512117],[-0.090432,51.51151],[-0.085969,51.5107],[-0.076546,51.509971],[-0.072384,51.515037],[-0.059971,51.519518],[-0.046596,51.521858],[-0.03364,51.525122],[-0.025128,51.52694],[-0.011538,51.524839],[0.005055,51.528136],[0.017451,51.531341],[0.035263,51.53534],[0.051186,51.538948],[0.081053,51.539321],[0.10153,51.538372],[0.127016,51.540331],[0.147527,51.541639],[0.166017,51.544096],[0.19864,51.549775],[0.219116,51.554093],[0.235809,51.55856],[0.250882,51.559063]]]"]
Unlike calls I am used to they don't come as individual lat and long values that I can iterate, it returns as a long String block.
I may be incorrect, but I don't believe I can parse it as there is no lat
& long
class to iterate, just lineStrings
which is a long block. My Quicktype.io class to parse takes me to .linestrings
when iterating
I want to split these into coordinate pairs that I can take the lat & long from each.
I have tried ```splitting```` but due to commas between the doubles & the pairs I couldn't get it to work. I also tried removing special characters but couldn't keep it formatted.
Any suggestions welcome.
End goal is the be able to perform something like the following
for (var x in coordinates){
GeoCoordinates y = GeoCoordinates(x.latitude, x.longitude);
}
Thanks
CodePudding user response:
You'll have to do some manually splitting:
void main() {
'[[[-0.301457,51.515017],[-0.288265,51.51014],[-0.280462,51.503057]]]'
.split('],[')
.forEach((p) => print(p.replaceAll('[', '').replaceAll(']', '')));
}
prints:
-0.301457,51.515017
-0.288265,51.51014
-0.280462,51.503057
which you can further split on ,
and parse as a pair of doubles.
CodePudding user response:
var json = {"lineStrings": [
"[[[-0.301457,51.515017],[-0.288265,51.51014],[-0.280462,51.503057],[-0.267972,51.494627],[-0.254555,51.495148],[-0.245704,51.494917],[-0.235881,51.494122],[-0.22362,51.4923],[-0.213427,51.490311],[-0.206636,51.490459],[-0.193378,51.492063],[-0.182658,51.494316],[-0.174138,51.494094],[-0.156377,51.49227],[-0.143102,51.496359],[-0.133608,51.499544],[-0.124861,51.50132],[-0.122666,51.507058],[-0.11426,51.511006],[-0.103659,51.511581],[-0.094009,51.512117],[-0.090432,51.51151],[-0.085969,51.5107],[-0.076546,51.509971],[-0.072384,51.515037],[-0.059971,51.519518],[-0.046596,51.521858],[-0.03364,51.525122],[-0.025128,51.52694],[-0.011538,51.524839],[0.005055,51.528136],[0.017451,51.531341],[0.035263,51.53534],[0.051186,51.538948],[0.081053,51.539321],[0.10153,51.538372],[0.127016,51.540331],[0.147527,51.541639],[0.166017,51.544096],[0.19864,51.549775],[0.219116,51.554093],[0.235809,51.55856],[0.250882,51.559063]]]"
]
};
List<dynamic> jsonList = json["lineStrings"]!
.first
.replaceAll("[", "")
.replaceAll("]", "")
.split(",");
List<Coordinates> result = [];
for (var i = 0; i < jsonList.length / 2; i ) {
result.add(Coordinates(
lat: double.parse(jsonList[i]),
long: double.parse(jsonList[i 1])));
}
result.forEach((element) {
print("Lat" element.lat.toString());
print("Long" element.long.toString());
print("*************************");
});
class Coordinates {
double lat;
double long;
Coordinates({
required this.lat,
required this.long,
});
}
flutter: Lat-0.301457
flutter: Long51.515017
flutter: *************************
flutter: Lat51.515017
flutter: Long-0.288265
flutter: *************************
flutter: Lat-0.288265
flutter: Long51.51014
flutter: *************************