使用GSON反序列化多个内部类(Deserializing Multiple inner classes using GSON)

编程入门 行业动态 更新时间:2024-10-11 23:17:56
使用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 } }]

这是我为使用java的gson使用的json对象创建的类的示例

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对象。 非常感谢你

I am trying Deserialize data from JSON object using gson. I am having trouble with 1)designing a class. 2) getting a null object from the inner list object.

Here is a sample of the JSON object

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

Here is a sample of the class i made for the json object to use with gson using java

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

Here is the sample code using gson

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

Any help would be appreciated and I am very new to GSON and retrieving JSON objects. Thank you very much

最满意答案

我会摆脱内部的类定义,除非有一些非常令人信服的理由来拥有它们。 如果命名空间是目标,那么至少使它们成为静态嵌套类定义。 这也将使它们更容易反序列化。

我在http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html#TOC-Nested-Classes-including-Inner-Clas上发布了反序列化到静态嵌套类和内部类的示例

I'd get rid of the inner class definitions, unless there were some very compelling reason to have them. If namespace is the goal, then at least make them static nested class definitions. This will also make them easier to deserialize into.

I posted examples of deserializing into static nested classes and inner classes at http://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html#TOC-Nested-Classes-including-Inner-Clas

更多推荐

本文发布于:2023-07-30 14:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部类   多个   序列化   GSON   Multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!