gson

Gson ignore null when deserializing object

筅森魡賤 提交于 2020-01-29 13:10:12
问题 I want to deserialize a json string that containts a null value in Java. I want to deserialize the object to a Properties object. The json string is something like: {"prop1":null, "propr2":"fancy value"} When I deserialize using String json = // new Gson().fromJson(json, Properties.class); I get a null pointer exception because of the Hastable that into the Properties object. How can I instruct Gson to ignore deserialization of null values? 回答1: We have this solution: 1. All of your data

如何使用意图将对象从一个Android活动发送到另一个?

柔情痞子 提交于 2020-01-29 08:06:54
如何使用 Intent 类的 putExtra() 方法将自定义类型的对象从一个 Activity 传递到另一个 Activity ? #1楼 感谢您的包裹帮助,但我发现了另一种可选解决方案 public class getsetclass implements Serializable { private int dt = 10; //pass any object, drwabale public int getDt() { return dt; } public void setDt(int dt) { this.dt = dt; } } 活动一 getsetclass d = new getsetclass (); d.setDt(50); LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>(); obj.put("hashmapkey", d); Intent inew = new Intent(SgParceLableSampelActivity.this, ActivityNext.class); Bundle b = new Bundle(); b.putSerializable("bundleobj", obj); inew.putExtras(b);

Gson和Fastjson序列学习

吃可爱长大的小学妹 提交于 2020-01-28 13:30:39
1、Gson序列化(只关注Field): 可以看出使用GSON序列化,没有getter和setter是可以的,只要有Field就可以,查看源代码可以知道Gson使用Field[] fields = raw.getDeclaredFields()来获取需要序列化的字段; getDeclaredFields可以获取对象的所有字段,但是 GSON排除了被transient和static修饰 的字段Field getDeclaredFields本身不能获取父类的字段,但是GSON内部处理是一直循环到Object对象,最终会序列化出该类及所有父类中的Field class Animal { private String name_a; private Integer age_a; public Animal(String name, Integer age) { this.name_a = name; this.age_a = age; } public String toString() { return "Animal [name_a=" + name_a + ", age_a=" + age_a + "]"; } } class Person extends Animal { private String name_p; private Integer age_p; public

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated obj

倾然丶 夕夏残阳落幕 提交于 2020-01-26 15:39:26
这个错误是由于操作的json字符串,使用string类型有误。 我要把object对象recomdList解析出来,应该是一个list<T>,代码里需要把recomdList转成string,我直接转报错,应该是转成json才对。JSONObject.toJSONString(recomdList) 一个object对象recomdList转成List<T>代码: if (recomdList instanceof String){ }else if (recomdList instanceof ArrayList<?>) { Gson gson = new Gson(); JsonArray arry = new JsonParser().parse(JSONObject.toJSONString(recomdList)).getAsJsonArray(); for (JsonElement jsonElement : arry) { recomd_list.add(gson.fromJson(jsonElement,ProductsRecommendListBean.class)); } } 来源: CSDN 作者: 那个游侠 链接: https://blog.csdn.net/shsh_0415/article/details/103964190

Convert Json date to java date

人走茶凉 提交于 2020-01-26 10:58:38
问题 My json respons contains a CreatedOn Date: { "CreatedOn" : "\/Date(1406192939581)\/" } I need to convert CreatedOn to a simple date format and count the days of difference from CreatedOn Date to Present Date. When I debug the below code string CreatedOn showing a null value. How come? JSONObject store = new JSONObject(response); if (response.contains("CreatedOn")) { String CreatedOn = store.getString("CreatedOn"); } 回答1: JSONObject store = new JSONObject(response); if(store.has("CreatedOn"))

Convert Json date to java date

非 Y 不嫁゛ 提交于 2020-01-26 10:54:06
问题 My json respons contains a CreatedOn Date: { "CreatedOn" : "\/Date(1406192939581)\/" } I need to convert CreatedOn to a simple date format and count the days of difference from CreatedOn Date to Present Date. When I debug the below code string CreatedOn showing a null value. How come? JSONObject store = new JSONObject(response); if (response.contains("CreatedOn")) { String CreatedOn = store.getString("CreatedOn"); } 回答1: JSONObject store = new JSONObject(response); if(store.has("CreatedOn"))

SpringBoot实战(十四)之整合KafKa

依然范特西╮ 提交于 2020-01-24 05:42:05
本人今天上午参考了不少博文,发现不少博文不是特别好,不是因为依赖冲突问题就是因为版本问题。 于是我结合相关的博文和案例,自己改写了下并参考了下,于是就有了这篇文章。希望能够给大家帮助,少走一些弯路。 一、KafKa的介绍 1.主要功能 根据官网的介绍,ApacheKafka®是 一个分布式流媒体平台 ,它主要有3种功能:   a.发布和订阅消息流,这个功能类似于消息队列,这也是kafka归类为消息队列框架的原因。   b.以容错的方式记录消息流,kafka以文件的方式来存储消息流。   c.可以再消息发布的时候进行处理。 2.使用场景 a.在系统或应用程序之间构建可靠的用于传输实时数据的管道,消息队列功能。 b.构建实时的流数据处理程序来变换或处理数据流,数据处理功能。 3.详细介绍 Kafka目前主要作为一个分布式的发布订阅式的消息系统使用,下面简单介绍一下kafka的基本机制 消息传输过程: Producer 即生产者,向Kafka集群发送消息,在发送消息之前,会对消息进行分类,即Topic,上图展示了两个producer发送了分类为topic1的消息,另外一个发送了topic2的消息。 Topic 即主题,通过对消息指定主题可以将消息分类,消费者可以只关注自己需要的Topic中的消息 Consumer 即消费者,消费者通过与kafka集群建立长连接的方式

Retrofit - Expected BEGIN_ARRAY but was BEGIN_OBJECT?

霸气de小男生 提交于 2020-01-24 00:46:49
问题 I am getting a json result from service with retrofit like bellow : { "result": { "totalCount": 15, "resultCount": 2, "offset": 0, "limit": 2, "products": [ { "id": 10081, "name": "prod", "pictureUrl": "url", "price": 1, "url": "url", "briefDescription": "test", "description": "test", "pictures": [], "categoryTitle": "s s", "categoryId": 53, "extraInfo": { "productProperties": [ { "id": 88, "value": "6", "measurementUnit": "s", "title": "s" }, { "id": 89, "value": "2", "measurementUnit": "s",

How can I serialize a custom object that has a Context as a private member using Gson?

China☆狼群 提交于 2020-01-23 17:44:05
问题 I have an ArrayList of custom objects that I'm trying to store to disk as a JSON String using Gson. This works great for my objects that just contain basic types of data like Strings, but one class I have has a private member that stores the Activity's Context. When I try to serialize this class using Gson, I get the following Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxx.xxxx/com.xxxx.xxxx.MainActivity}: java.lang.SecurityException: Can't make method

gson.fromJson(json, new TypeToken<List<City>

主宰稳场 提交于 2020-01-23 02:54:59
private List<City> loadCities() { // In this case we're loading from local assets. // NOTE: could alternatively easily load from network. // However, that would need to happen on a background thread. InputStream stream; try { stream = getAssets().open("cities.json"); } catch (IOException e) { return null; } Gson gson = new GsonBuilder().create(); JsonElement json = new JsonParser().parse(new InputStreamReader(stream)); return gson.fromJson(json, new TypeToken<List<City>>() { }.getType()); } 来源: https://www.cnblogs.com/endv/p/12230076.html