簡體   English   中英

使用GSON反序列化多個內部類

[英]Deserializing Multiple inner classes using GSON

我正在嘗試使用gson從JSON對象反序列化數據。 我在設計課程時遇到了麻煩。 2)從內部列表對象獲取空對象。

以下是JSON對象的示例

{
  "spatialReference" : {
    "wkid" : 102113
  },
  "candidates" : [
    {
      "address" : "202 S Van Ness Ave, San Francisco, CA, 94110",
      "location" : {
        "x" : -13627444.2697,
        "y" : 4546249.2471000031
      },
      "score" : 85.969999999999999,
      "attributes" : {
        "Loc_name" : "US_RoofTop",
        "Score" : 85.969999999999999,
        "Match_addr" : "505 S Van Ness Ave, San Francisco, CA, 94110",
        "House" : "505",
        "Side" : "R",
        "PreDir" : "S",
        "PreType" : "",
        "StreetName" : "Van Ness",
        "SufType" : "Ave",
        "SufDir" : "",
        "City" : "San Francisco",
        "State" : "CA",
        "ZIP" : "94110",
        "X" : -122.417416,
        "Y" : 37.764772999999998,
        "Disp_Lon" : -122.416991,
        "Disp_Lat" : 37.764809999999997,
        "Addr_type" : "StreetAddress",
        "Province" : "",
        "Postal" : "",
        "FromAddr" : "",
        "ToAddr" : "",
        "ZIP4" : "",
        "ZIP4_TYPE" : "",
        "User_fld" : "",
        "Ldu" : "",
        "xmin" : 0,
        "xmax" : 0,
        "ymin" : 0,
        "ymax" : 0
      }
    },
    {
      "address" : "505 Van Ness Ave, San Francisco, CA, 94102",
      "location" : {
        "x" : -13627778.172800001,
        "y" : 4548412.0926000029
      },
      "score" : 100,
      "attributes" : {
        "Loc_name" : "US_Streets",
        "Score" : 100,
        "Match_addr" : "505 Van Ness Ave, San Francisco, CA, 94102",
        "House" : "",
        "Side" : "L",
        "PreDir" : "",
        "PreType" : "",
        "StreetName" : "Van Ness",
        "SufType" : "Ave",
        "SufDir" : "",
        "City" : "San Francisco",
        "State" : "CA",
        "ZIP" : "94102",
        "X" : -122.42041500000001,
        "Y" : 37.780130999999997,
        "Disp_Lon" : 0,
        "Disp_Lat" : 0,
        "Addr_type" : "StreetAddress",
        "Province" : "",
        "Postal" : "",
        "FromAddr" : "501",
        "ToAddr" : "525",
        "ZIP4" : "",
        "ZIP4_TYPE" : "",
        "User_fld" : "",
        "Ldu" : "",
        "xmin" : 0,
        "xmax" : 0,
        "ymin" : 0,
        "ymax" : 0
      }
    }]

這是我為json對象使用java與gson一起使用的類的示例

    public class Response {

    public Response()
    {}

    SpatialReference spatial;

    public List<Candidates> candidate;

    public class Candidates
    {
        public Candidates()
        {}

        @SerializedName("address")
        public String address;

        Location location;

        @SerializedName("score")
        public double score;

        Attribute attributes;

        Double getScore()
        {
            return score;
        }

    }

    public class Attribute {

        public Attribute()
        {}

        @SerializedName("Disp_Lon")
        public double dispLong;

        @SerializedName("Disp_Lat")
        public double dispLat;

    }

    public class Location {

        public Location()
        {}

        @SerializedName("x")
        public double x;

        @SerializedName("y")
        public double y;

    }

    public class SpatialReference {

        SpatialReference()
        {}

        @SerializedName("wkid")
        public String wkid;

    }

}

這是使用gson的示例代碼

        Gson gson= new Gson()

        response1= gson.fromJson(reader, Response.class);

        return response1;

任何幫助將不勝感激,我是GSON的新手並檢索JSON對象。 非常感謝你

我會擺脫內部的類定義,除非有一些非常令人信服的理由來擁有它們。 如果命名空間是目標,那么至少使它們成為靜態嵌套類定義。 這也將使它們更容易反序列化。

我在http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html#TOC-Nested-Classes-including-Inner-Clas上發布了反序列化到靜態嵌套類和內部類的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM