Reputation: 45
I have an array of coordinates from JSON. It looks like this.
[ [ [ [ 18.446000, -33.921060 ], [ 18.446020, -33.923410 ], [ 18.444510, -33.923620 ], [ 18.443350, -33.923990 ], [ 18.443350, -33.924360 ], [ 18.443360, -33.924970 ], [ 18.443230, -33.924970 ]]]]
I cannot work out how to get the length of the inner set of values [Lat,Lng],[Lat,Lng]... so I can loop through them. In python I would do something like:
for value in values[0][0]:
print value
What would the equivalent in java be? I think I am getting massively confused with arrays in Java. I'm trying to get at them using something like this at the moment, but to be honest I'm shooting in the dark here.
List<JSONObject> geocords = Arrays.asList(cords.getJSONObject(0))
I've tried things like:
for (i=0; HOW TO GET LENGTH OF INNER SET OF VALUES?; i++){
}
EDIT
List<JSONObject> geocords = Arrays.asList(cords.getJSONObject(0))
Throws a JSONException.
If I change it to:
List<JSONArray> geocords = Arrays.asList(cords.getJSONArray(0));
Then geocords outputs as:
[[[[18.49576,-34.01209],[18.49447,-34.01473],[18.49327,-34.01705],[18.48914,-34.02501],[18.48866,-34.02592],[18.48873,-34.02612],[18.48912,-34.027],[18.48917,-34.02711],[18.49,-34.02899],[18.49003,-34.02976],[18.49031,-34.03164],[18.49051,-34.03264],[18.49093,-34.03462],[18.49152,-34.03742],[18.4917,-34.03772],[18.4918,-34.03782],[18.49186,-34.038],[18.49184,-34.03823],[18.49159,-34.0388],[18.49105,-34.03986],[18.49041,-34.04115],[18.48984,-34.04113],[18.4891,-34.04246],[18.48784,-34.04214],[18.48791,-34.04326],[18.48786,-34.04356],[18.48754,-34.04391],[18.48738,-34.04391],[18.48713,-34.04399],[18.48693,-34.04405],[18.48677,-34.04407],[18.48665,-34.04396],[18.4865,-34.0439],[18.48631,-34.04396],[18.48611,-34.04408],[18.48598,-34.04427],[18.48599,-34.04456],[18.48595,-34.04487],[18.48583,-34.04508],[18.48557,-34.04531],[18.48527,-34.04543],[18.48496,-34.04547],[18.48446,-34.04553],[18.48418,-34.04498],[18.48342,-34.04371],[18.48335,-34.0436],[18.48267,-34.04247],[18.47827,-34.04429],[18.47818,-34.04436],[18.47645,-34.04146],[18.47652,-34.04122],[18.47662,-34.04039],[18.47632,-34.03942],[18.47611,-34.03916],[18.47611,-34.0391],[18.47589,-34.03756],[18.47588,-34.03748],[18.47438,-34.03715],[18.47389,-34.0368],[18.47308,-34.03643],[18.47103,-34.03727],[18.47053,-34.03757],[18.47029,-34.03773],[18.46953,-34.03846],[18.46925,-34.03861],[18.46915,-34.03867],[18.46899,-34.03872],[18.46877,-34.03879],[18.4683,-34.03884],[18.46806,-34.03887],[18.46791,-34.03889],[18.46757,-34.03897],[18.46747,-34.03906],[18.46728,-34.03925],[18.46711,-34.03945],[18.46698,-34.03967],[18.46685,-34.03992],[18.46672,-34.04016],[18.46664,-34.04102],[18.46656,-34.04251],[18.46648,-34.04292],[18.46637,-34.04356],[18.46617,-34.04355],[18.46585,-34.0435],[18.46575,-34.04348],[18.46561,-34.04346],[18.46538,-34.04342],[18.46515,-34.04339],[18.46484,-34.04334],[18.46447,-34.04328],[18.46396,-34.0432],[18.4636,-34.04314],[18.46366,-34.04288],[18.46374,-34.04253],[18.46237,-34.04232],[18.46158,-34.0422],[18.4613,-34.04216],[18.45996,-34.04196],[18.45925,-34.04185],[18.45863,-34.04176],[18.45756,-34.0416],[18.45656,-34.04144],[18.45602,-34.04136],[18.45571,-34.04131],[18.456,-34.03991],[18.45458,-34.03972],[18.45408,-34.03965],[18.45373,-34.03968],[18.45337,-34.03972],[18.45303,-34.03981],[18.45042,-34.0406],[18.45022,-34.04002],[18.45018,-34.03994],[18.45005,-34.03972],[18.44967,-34.03905],[18.44912,-34.03807],[18.44876,-34.03742],[18.44861,-34.03717],[18.44854,-34.03702],[18.44843,-34.03683],[18.44822,-34.03646],[18.44812,-34.03628],[18.44801,-34.0361],[18.44791,-34.0359],[18.44781,-34.03573],[18.4476,-34.03535],[18.44752,-34.03521],[18.44737,-34.03495],[18.44723,-34.03471],[18.44709,-34.03444],[18.44698,-34.03425],[18.44687,-34.03405],[18.44669,-34.03374],[18.44635,-34.03313],[18.44625,-34.03295],[18.44613,-34.03272],[18.446,-34.03251],[18.4459,-34.0323],[18.44576,-34.03205],[18.44516,-34.031],[18.44408,-34.029],[18.44385,-34.02882],[18.44376,-34.02921],[18.4437,-34.02938],[18.44364,-34.02947],[18.44328,-34.02994],[18.44296,-34.03014],[18.44236,-34.03017],[18.44179,-34.03008],[18.44168,-34.03007],[18.44054,-34.02989],[18.44019,-34.02988],[18.44001,-34.02992],[18.43968,-34.02999],[18.43951,-34.03006],[18.4394,-34.03012],[18.43935,-34.03017],[18.43849,-34.03086],[18.43808,-34.03122],[18.43759,-34.03164],[18.43714,-34.032],[18.43695,-34.03215],[18.43645,-34.03234],[18.43585,-34.03256],[18.43542,-34.03272],[18.43464,-34.033],[18.43413,-34.03319],[18.4336,-34.03335],[18.43299,-34.0335],[18.43272,-34.03353],[18.43235,-34.03358],[18.43147,-34.03363],[18.43134,-34.03364],[18.43102,-34.03367],[18.43065,-34.03373],[18.4299,-34.03394],[18.42951,-34.03404],[18.42911,-34.03414],[18.42893,-34.03415],[18.42873,-34.03418],[18.42845,-34.03422],[18.42816,-34.03429],[18.42647,-34.03472],[18.42627,-34.03478],[18.42608,-34.0349],[18.4248,-34.03586],[18.42294,-34.03728],[18.42202,-34.03798],[18.42184,-34.03806],[18.42168,-34.03813],[18.42137,-34.03811],[18.42104,-34.03806],[18.42068,-34.03799],[18.41975,-34.03781],[18.41935,-34.03786],[18.41911,-34
Upvotes: 0
Views: 448
Reputation: 4213
Are you sure the four square brackets at the beginning are ok? If yes, then this should work:
// get the array of coordinates buried two levels down of singleton arrays
JSONArray array = coord.getJSONArray(0).getJSONArray(0);
// iterate through the array of coordinates
for (int i = 0; i < array.length(); i++) {
JSONArray inside = array.getJSONArray(i);
for (int j = 0; j < inside.length(); j++) {
System.out.println(inside.getDouble(j));
}
}
I am not sure why json.org does not provide a more friendly interface (like iterable)
Upvotes: 3
Reputation:
You would loop through the inner set like so in Java:
for (int i=0;i<array.length;i+=1)
{
for (int j=0;j<array[i].length;j+=1)
{
System.out.println(array[i][j]);
}
}
Alternatively, i
could be hardcoded to the index that you want to run through.
Upvotes: 0