Java Gson - 帮我解析我的JSON

时间:2011-07-08 08:25:08

标签: java android json gson

我是GSON的新手,我真的很难解析这些数据:

{
   "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",
         "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"
      }
   }
}

这是我所熟悉的,但我认为这是错误的......

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);

这是我遇到的错误:

> 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)

任何人都可以指出我正确的方向吗?

非常感谢,

4 个答案:

答案 0 :(得分:2)

No-args constructor for class ie.clarity.ShowJobsOnMap$GPSObject does not exist表示找不到构造函数,很可能是因为它在您的情况下不可见。将public关键字添加到构造函数中:public GPSObject() { ... }而不是GPSObject() { ... }

编辑:GPSObject类似乎嵌套在ShowJobsOnMap中,如错误消息所示。 您现在将GPSObject.class传递给GSON,但是要构造非静态嵌套类的对象,首先需要封闭类的对象。为了解决这个问题,你必须声明嵌套类是静态的,从而删除嵌套类和封闭类之间的特殊关系。然后,封闭类只具有嵌套类的视角命名空间的功能。

答案 1 :(得分:2)

我尝试在不使用GSON的情况下解析此JSON,并且我能够解析此JSON字符串。请尝试这个作为替代解决方案。我正在详细说明我用于解析此字符串的代码片段:

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();
            }
        }

答案 2 :(得分:2)

这是使用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;
}

输出:

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}}
...

答案 3 :(得分:1)

看看这是否对您有用Getting data from JSON

您可以在主帖中发布课程代码吗?