开发者

Java Gson - help me parse my JSON

开发者 https://www.devze.com 2023-03-18 00:26 出处:网络
I\'m new to GSON and am really struggling to parse this data: { \"0\":{ \"county\":\"Carlow\", \"coords\":{

I'm new to GSON and am really struggling to parse this data:

{
   "0":{
      "county":"Carlow",
      "coords":{
         "lat":"52.72465",
         "lng":"-6.92963"
      }
   },
   "1":{
      "county":"Cavan",
      "coords":{
         "lat":"53.9011",
         "lng":"-7.34436"
      }
   },
   "2":{
      "county":"Clare",
      "coords":{
         "lat":"52.73629",
         "lng":"-8.97583"
      }
   },
   "3":{
      "county":"Cork",
      "coords":{
         "lat":"51.78823",
         "lng":"-8.46771"
      }
   },
   "4":{
      "county":"Dublin North County",
      "coords":{
         "lat":"51.78823",
         "lng":"-6.174316"
      }
   },
   "5":{
      "county":"Dublin South County",
      "coords":{
         "lat":"53.272605",
         "lng":"-6.166077"
      }
   },
   "6":{
      "county":"Dublin 1",
      "coords":{
         "lat":"53.62798",
         "lng":"-8.188635"
      }
   },
   "7":{
      "county":"Dublin 2",
      "coords":{
         "lat":"53.352996",
         "lng":"-6.24637"
      }
   },
   "8":{
      "county":"Dublin 3",
      "coords":{
         "lat":"53.338166",
         "lng":"-6.336515"
      }
   },
   "9":{
      "county":"Dublin 4",
      "coords":{
         "lat":"53.368377",
         "lng":"-6.18873"
      }
   },
   "10":{
      "county":"Dublin 5",
      "coords":{
         "lat":"53.311391",
         "lng":"-6.219978"
      }
   },
   "11":{
      "county":"Dublin 6",
      "coords":{
         "lat":"53.385938",
         "lng":"-6.193697"
      }
   },
   "12":{
      "county":"Dublin 7",
      "coords":{
         "lat":"53.349219",
         "lng":"-6.289244"
      }
   },
   "13":{
      "county":"Dublin 8",
      "coords":{
         "lat":"53.350221",
         "lng":"-6.282517"
      }
   },
   "14":{
      "county":"Dublin 9",
      "coords":{
         "lat":"53.351576",
         "lng":"-6.284523"
      }
   },
   "15":{
      "county":"Dublin 10",
      "coords":{
         "lat":"53.3866058",
         "lng":"-6.23542"
      }
   },
   "16":{
      "county":"Dublin 11",
      "coords":{
         "lat":"53.342557",
         "lng":"-6.358278"
      }
   },
   "17":{
      "county":"Dublin 12",
      "coords":{
         "lat":"53.34968"开发者_如何学JAVA,
         "lng":"-6.24723"
      }
   },
   "18":{
      "county":"Dublin 13",
      "coords":{
         "lat":"53.33d3918"
      }
   },
   "19":{
      "county":"Dublin 15",
      "coords":{
         "lat":"53.382076",
         "lng":"-6.366707"
      }
   },
   "20":{
      "county":"Dublin 16",
      "coords":{
         "lat":"53.382076",
         "lng":"-6.366707"
      }
   },
   "21":{
      "county":"Dublin 17",
      "coords":{
         "lat":"53.310775",
         "lng":"-6.242981"
      }
   },
   "22":{
      "county":"Dublin 18",
      "coords":{
         "lat":"53.407485",
         "lng":"-6.205215"
      }
   },
   "23":{
      "county":"Dublin 20",
      "coords":{
         "lat":"53.407485",
         "lng":"-6.205215"
      }
   },
   "24":{
      "county":"Dublin 22",
      "coords":{
         "lat":"53.355403",
         "lng":"-6.371452"
      }
   },
   "25":{
      "county":"Dublin 24",
      "coords":{
         "lat":"53.299391",
         "lng":"-6.390263"
      }
   },
   "26":{
      "county":"Galway",
      "coords":{
         "lat":"53.17477",
         "lng":"-9.03625"
      }
   },
   "27":{
      "county":"Kerry",
      "coords":{
         "lat":"52.1554",
         "lng":"-9.69269"
      }
   },
   "28":{
      "county":"Kildare",
      "coords":{
         "lat":"53.159829",
         "lng":"-6.908998"
      }
   },
   "29":{
      "county":"Kilkenny",
      "coords":{
         "lat":"52.54296",
         "lng":"-7.23175"
      }
   },
   "30":{
      "county":"Laois",
      "coords":{
         "lat":"52.88571",
         "lng":"-7.31689"
      }
   },
   "31":{
      "county":"Lietrim",
      "coords":{
         "lat":"53.88815",
         "lng":"-8.07495"
      }
   },
   "32":{
      "county":"Limerick",
      "coords":{
         "lat":"52.55465",
         "lng":"-8.65448"
      }
   },
   "33":{
      "county":"Longford",
      "coords":{
         "lat":"53.61695",
         "lng":"-7.77832"
      }
   },
   "34":{
      "county":"Louth",
      "coords":{
         "lat":"53.92699",
         "lng":"-6.53893"
      }
   },
   "35":{
      "county":"Mayo",
      "coords":{
         "lat":"53.74546",
         "lng":" -9.29169"
      }
   },
   "36":{
      "county":"Meath",
      "coords":{
         "lat":"53.45371",
         "lng":" -6.7923"
      }
   },
   "37":{
      "county":"Monaghan",
      "coords":{
         "lat":"54.13831",
         "lng":"-6.97083"
      }
   },
   "38":{
      "county":"Offaly",
      "coords":{
         "lat":"53.15501",
         "lng":"-7.74261"
      }
   },
   "39":{
      "county":"Roscommon",
      "coords":{
         "lat":"53.5762",
         "lng":" -8.19855"
      }
   },
   "40":{
      "county":"Sligo",
      "coords":{
         "lat":"54.16887",
         "lng":"-8.47321"
      }
   },
   "41":{
      "county":"Tipperary",
      "coords":{
         "lat":"52.36386",
         "lng":"-8.16833"
      }
   },
   "42":{
      "county":"Waterford",
      "coords":{
         "lat":"52.10988",
         "lng":"-7.12189"
      }
   },
   "43":{
      "county":"Westmeath",
      "coords":{
         "lat":"53.34727",
         "lng":"-7.46521"
      }
   },
   "44":{
      "county":"Wexford",
      "coords":{
         "lat":"52.2278",
         "lng":"-6.48193"
      }
   },
   "45":{
      "county":"Wicklow",
      "coords":{
         "lat":"53.08286",
         "lng":"-6.07904"
      }
   }
}

Here's what I have sofar, but I think it's wrong...

public class GPSObject {
        private String county;
        private Coords coords;
        GPSObject() {
            // no-args constructor
        }
        public class Coords{
            public String lat;
            public String lng;
            public Coords() {
              // No args constructor for B
            }
        } 
    }


Gson gson=new Gson();
GPSObject obj2=gson.fromJson(response,GPSObject.class);

Here's an error I'm getting:

> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763): FATAL
> EXCEPTION: main 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):
> java.lang.RuntimeException: No-args
> constructor for class
> ie.clarity.ShowJobsOnMap$GPSObject
> does not exist. Register an
 > InstanceCreator with Gson for this
> type to fix this problem. 07-08
> 09:26:08.731:
> ERROR/AndroidRuntime(26763): atcom.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):     at
> com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):     at
>         com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:41)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):     at
> com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
> 07-08 09:26:08.731:
> ERROR/AndroidRuntime(26763):     at
> com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)

Can anyone please point me in the right direction?

Many thanks in advance,


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: 46
0: {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}}
...


See if this may help you Getting data from JSON

Can you post the codes of your classes in your main post?

0

精彩评论

暂无评论...
验证码 换一张
取 消