我从我的android应用程序使用API,所有的JSON响应是这样的: { 'status': 'OK', 'reason': 'Everything was fine', 'content': { < some data here > } 问题是我所有的POJO都有一个status , reason字段,而content字段里面是我想要的真正的POJO。 有什么办法可以创build一个Gson自定义转换器来提取总是content字段,所以改造返回适当的POJO?
我想通过谷歌Gson传递一个列表对象,但我不知道如何反序列化genericstypes。 我看了这个 (BalusC的回答)之后试了一下: MyClass mc = new Gson().fromJson(result, new List<MyClass>(){}.getClass()); 但是后来我在eclipse中得到一个错误:“新的List(){}types必须实现inheritance的抽象方法…”,如果我使用快速修复,我会得到一个超过20个方法存根的怪物。 我很确定有一个更简单的解决scheme,但我似乎无法find它! 编辑: 我现在有 Type listType = new TypeToken<List<MyClass>>() { }.getType(); MyClass mc = new Gson().fromJson(result, listType); 不过,我在“fromJson”一行中得到以下例外: java.lang.NullPointerException at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47) at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83) at java.lang.StringBuilder.append(StringBuilder.java:203) at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56) at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88) at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76) at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106) at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64) at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49) at com.google.gson.Gson.fromJson(Gson.java:568) at com.google.gson.Gson.fromJson(Gson.java:515) at com.google.gson.Gson.fromJson(Gson.java:484) at com.google.gson.Gson.fromJson(Gson.java:434) […]
(这篇文章是一个典型的问题 ,下面提供了一个示例答案。) 我试图反序列化一些JSON内容到Gson#fromJson(String, Class)的自定义POJOtypes。 这段代码 import com.google.gson.Gson; public class Sample { public static void main(String[] args) { String json = "{\"nestedPojo\":[{\"name\":null, \"value\":42}]}"; Gson gson = new Gson(); gson.fromJson(json, Pojo.class); } } class Pojo { NestedPojo nestedPojo; } class NestedPojo { String name; int value; } 抛出以下exception Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but […]
我想能够从我的Java操作方法中的JSONstring访问属性。 该string可以通过简单地说myJsonString = object.getJson() 。 下面是这个string的例子: { 'title': 'ComputingandInformationsystems', 'id': 1, 'children': 'true', 'groups': [{ 'title': 'LeveloneCIS', 'id': 2, 'children': 'true', 'groups': [{ 'title': 'IntroToComputingandInternet', 'id': 3, 'children': 'false', 'groups': [] }] }] } 在这个string中,每个JSON对象都包含其他JSON对象的数组。 目的是提取任何给定对象拥有包含其他JSON对象的组属性的ID列表。 我把Google的Gson视为一个潜在的JSON插件。 任何人都可以提供某种forms的指导,如何从这个JSONstring中生成Java?
我有一个Java类,用户: public class User { int id; String name; Timestamp updateDate; } 我收到一个包含来自webservice的用户对象的JSON列表: [{"id":1,"name":"Jonas","update_date":"1300962900226"}, {"id":5,"name":"Test","date_date":"1304782298024"}] 我试图写一个自定义的反序列化器: @Override public User deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { return new User( json.getAsJsonPrimitive().getAsInt(), json.getAsString(), json.getAsInt(), (Timestamp)context.deserialize(json.getAsJsonPrimitive(), Timestamp.class)); } 但是我的反序列化器不起作用。 我怎样才能为Gson编写一个自定义的JSON解串器?
我有这个代码: Type typeOfObjectsList = new TypeToken<ArrayList<myClass>>() {}.getType(); List<myClass> objectsList = new Gson().fromJson(json, typeOfObjectsList); 它将JSON字符串转换为对象List 。 但是现在我想要在运行时定义一个动态类型的ArrayList (不只是myClass )。 ArrayList的项目类型将被反射定义。 我试过这个: private <T> Type setModelAndGetCorrespondingList2(Class<T> type) { Type typeOfObjectsListNew = new TypeToken<ArrayList<T>>() {}.getType(); return typeOfObjectsListNew; } 但它不起作用。 这是例外: java.sql.SQLException: Fail to convert to internal representation: {….my json….}
我想解析来自JSON类型为String 。 我正在使用Google Gson 。 我有: jsonLine = " { "data": { "translations": [ { "translatedText": "Hello world" } ] } } "; 而我的课是: public class JsonParsing{ public void parse(String jsonLine) { // there I would like to get String "Hello world" } }
我正在从一个返回JSON格式的数据的服务器请求数据。 提出请求时,将HashMap转换为JSON并不难,但另一方面似乎有点棘手。 JSON响应如下所示: { "header" : { "alerts" : [ { "AlertID" : "2", "TSExpires" : null, "Target" : "1", "Text" : "woot", "Type" : "1" }, { "AlertID" : "3", "TSExpires" : null, "Target" : "1", "Text" : "woot", "Type" : "1" } ], "session" : "0bc8d0835f93ac3ebbf11560b2c5be9a" }, "result" : "4be26bc400d3c" } 访问这些数据最简单的方法是什么? 我正在使用GSON模块。
我试图解析一个像这样的JSON字符串 [ { "updated_at":"2012-03-02 21:06:01", "fetched_at":"2012-03-02 21:28:37.728840", "description":null, "language":null, "title":"JOHN", "url":"http://rus.JOHN.JOHN/rss.php", "icon_url":null, "logo_url":null, "id":"4f4791da203d0c2d76000035", "modified":"2012-03-02 23:28:58.840076" }, { "updated_at":"2012-03-02 14:07:44", "fetched_at":"2012-03-02 21:28:37.033108", "description":null, "language":null, "title":"PETER", "url":"http://PETER.PETER.lv/rss.php", "icon_url":null, "logo_url":null, "id":"4f476f61203d0c2d89000253", "modified":"2012-03-02 23:28:57.928001" } ] 到对象列表中。 List<channelSearchEnum> lcs = (List<channelSearchEnum>) new Gson().fromJson( jstring , channelSearchEnum.class); 这是我正在使用的一个对象类。 import com.google.gson.annotations.SerializedName; public class channelSearchEnum { @SerializedName("updated_at") private String updated_at; @SerializedName("fetched_at") […]
我正在努力学习Gson,而我正在努力解决现场排斥问题。 这是我的课程 public class Student { private Long id; private String firstName = "Philip"; private String middleName = "J."; private String initials = "PF"; private String lastName = "Fry"; private Country country; private Country countryOfBirth; } public class Country { private Long id; private String name; private Object other; } 我可以使用GsonBuilder并添加ExclusionStrategy像字段名称像firstName或country但我似乎无法设法排除像country.name这样的特定字段的属性。 使用方法public boolean shouldSkipField(FieldAttributes fa) […]