0

I want to convert my JsonObject into an ArrayList of latitude and longitude. I was striving with this problem three days without any result. Please help!

Here's my JSON:

{
   "hits":[
      {
         "_geoloc":{
            "lat":33.842624,
            "lng":-5.480229
         },
         "objectID":"-KsPQUlvJK-PCSrH3Dq3"
      },
      {
         "_geoloc":{
            "lat":33.84878,
            "lng":-5.481698
         },
         "objectID":"-KsPQUloi4BAduevPJta"
      }
   ],
   "nbHits":2,
   "page":0,
   "nbPages":1,
   "hitsPerPage":20,
   "processingTimeMS":1,
   "exhaustiveNbHits":true,
   "query":"",
   "params":"aroundLatLng=33.62727138006218%2C-4.498191252350807&aroundRadius=545000"
}

Here's my code:

client.getIndex("users").searchAsync(query, new CompletionHandler() {
    @Override
    public void requestCompleted(JSONObject jsonObject, AlgoliaException e) {
    }
});
juzraai
  • 5,693
  • 8
  • 33
  • 47
john
  • 39
  • 2
  • 7
  • Do you have a corresponding java model as well? – Naman Aug 27 '17 at 21:39
  • 1
    Create a corresponding model (POJO) and let the JSON library do it for you. There are only 2 times you want to manually parse a JSON tree : 1) You are paid by the hour 2) You've a special use case the default deserialization can't handle. I don't know about #1, but #2 doesn't seem to apply here. – Abhijit Sarkar Aug 27 '17 at 23:44
  • Here are quite few solutions defined depending on which library you have access to https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java It is quite possible a duplicate question – Geek Aug 28 '17 at 02:05

2 Answers2

0

You can use the member methods of JsonObject to do this (http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html). For example, something like the following:

class GeoPoint {
    private double lat;
    private double lon;
    public GeoPoint( double lat, double lon ) {
        this.lat = lat
        this.lon = lon
    }
    // ...
}
ArrayList<GeoPoint> list = new ArrayList<>();
// Thankfully JsonArray objects are iterable...
for (JsonValue e : jsonObject.getJsonArray("hits")) {
    JsonObject coordWrapper = (JsonObject) e;
    JsonObject coord = coordWrapper.getJsonObject("_geoloc");
    double lat = coord.getJsonNumber("lat").doubleValue();
    double lon = coord.getJsonNumber("lon").doubleValue();
    list.add(new GeoPoint(lat, lon));
}
0

If you use fastjson as your JSON lib, you can do it like this:

public class GeoLoc {
    private double lng;
    private double lat;
    // getter & setter...
}

public class Geo {
    private String objectID;

    @JSONField(name = "_geoloc")
    private GeoLoc geoLoc;
    // getter & setter...
}

public static void main(String[] args) {
    String jsonStr = ""; // Your json string here.
    JSONObject jsonObject = JSON.parseObject(json);
    JSONArray jsonArray = jsonObject.getJSONArray("hits");
    List<Geo> geos = jsonArray.toJavaList(Geo.class);
    List<GeoLoc> geoLocs = geos.stream()
            .map(Geo::getGeoLoc)
            .collect(Collectors.toList());
}

No need to transform to List explicitly, fastjson do it for you.

Robyn Liu
  • 241
  • 2
  • 6