Java Gson - help me parse my JSON Java Gson - help me parse my JSON json json

Java Gson - help me parse my JSON


I tried to parse this JSON without using the GSON and I was able to parse this JSON string. Please try this also as a alternative solution. I am detailing snippet of the code that I have used to parse this string:

for(int i=0; i<46; i++){            try {                JSONObject jsonObject   =   new JSONObject(<string to be parsed>);                String first            =   jsonObject.getString(Integer.toString(i));                JSONObject jsonObject1  =   new JSONObject(first);                String country          =   jsonObject1.getString("county");                System.out.println("country="+country);                String cod              =   jsonObject1.getString("coords");                JSONObject jsonObject2  =   new JSONObject(cod);                String lat              =   jsonObject2.getString("lat");                System.out.println("lat="+lat);                String lng              =   jsonObject2.getString("lng");                System.out.println("lng="+lng);            } catch (JSONException e) {                e.printStackTrace();            }        }


No-args constructor for class ie.clarity.ShowJobsOnMap$GPSObject does not exist indicates the constructor is not found, most probably because it's not visible in your case. Add the public keyword to the constructor: public GPSObject() { ... } instead of GPSObject() { ... }

Edit: the GPSObject class seems to be nested in ShowJobsOnMap as indicated by the error message.You now pass GPSObject.class to GSON, however to construct an object of a non-static nested class, you first need an object of the enclosing class. In order to solve that, you'd have to declare the nested class static and thus remove the special relationship between the nested and the enclosing class. The enclosing class then just has the function of a namespace for the nested class' point of view.


Here's one simple approach using Gson.

import java.io.FileReader;import java.lang.reflect.Type;import java.util.Map;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;public class Foo{  public static void main(String[] args) throws Exception  {    Gson gson = new Gson();    Type gpsMapType = new TypeToken<Map<Integer, GPSObject>>() {}.getType();    Map<Integer, GPSObject> gpsObjects = gson.fromJson(new FileReader("input.json"), gpsMapType);    System.out.println("GPSObjects Count: " + gpsObjects.size());    for (Integer key : gpsObjects.keySet())    {      GPSObject gpsObject = gpsObjects.get(key);      System.out.printf("%d: {county: %s, coords:{lat:%s, %s}}\n",         key, gpsObject.county, gpsObject.coords.lat, gpsObject.coords.lng);    }  }}class GPSObject{  String county;  Coords coords;}class Coords{  String lat;  String lng;}

Output:

GPSObjects Count: 460: {county: Carlow, coords:{lat:52.72465, -6.92963}}1: {county: Cavan, coords:{lat:53.9011, -7.34436}}2: {county: Clare, coords:{lat:52.73629, -8.97583}}3: {county: Cork, coords:{lat:51.78823, -8.46771}}...