gson

Set POJO for Gson when JSON key has a dash

﹥>﹥吖頭↗ 提交于 2020-02-01 04:07:05
问题 The JSON string is: { "translation": ["some words"], "basic": { "us-phonetic": "'flæbɚɡæstɪd", "phonetic": "'flæbɚɡæstɪd", "uk-phonetic": "'flæbəga:stid", "explains": ["v. some words", "adj. some words" ] } } But Java can not have a value with a "-" in it. So how to get "us-phonetic" ? 回答1: Create a POJO class to represent your JSON and decorate your fields with the SerializedName annotation. gson uses @SerializedName("json_name") when the name of the JSON field and the name of the java field

Gson处理时间类型数据

孤街醉人 提交于 2020-01-31 10:22:25
Java与mysql中时间类型对应关系 mysql时间类型 Java时间类型 date java.sql.Date datetime java.sql.Timestamp timestamp java.sql.Timestamp time java.sql.Time year java.sql.Date 使用List<Map<String, Object>> map = getJdbcSupport().getJt().queryForList(...)从数据库中查询出数据后,需要转成JSON字符串。由于数据库(mysql)字段中有date和datetime类型的时间数据,直接使用gson.toJson(map)解析时时间数据不能正常解析,可以使用如下类型适配进行处理 /** * gson使用toJson时处理数据库中的datetime类型的时间数据 * * @author jinjinwang <br> * created on Jun 9, 2013 11:47:05 AM */ private class TimestampTypeAdapter implements JsonSerializer<Timestamp>, JsonDeserializer<Timestamp> { private final DateFormat format = new

Gson--Gson解析=等号出现乱码?

你说的曾经没有我的故事 提交于 2020-01-31 10:22:03
GSON使用笔记(1) -- 序列化时排除字段的几种方式 --3,关于Gson解析时候特殊符号,被转义的问题,如’单引号? //Creating the JSON object, and getting as String: JsonObject json = new JsonObject(); JsonObject inner = new JsonObject(); inner.addProperty("value", "xpath('hello')"); json.add("root", inner); System.out.println(json.toString()); //Trying to pretify JSON String: Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser parser = new JsonParser(); JsonElement je = parser.parse(json.toString()); System.out.println(gson.toJson(je)); 输出 {"root":{"value":"xpath('hello')"}} { "root": { "value": "xpath(\u0027hello\u0027)" } }

Spring Boot中解析JSON数据的三种方案

跟風遠走 提交于 2020-01-31 07:47:54
文章目录 JSON简介 一、Spring Boot默认的JSON解析 (1)使用示例 (2)修改特定数据的全局JSON格式 1.自定义MappingJackson2HttpMessageConverter 2.自定义ObjectMapper 二、使用Gson处理JSON (1)使用示例 (2)修改特定数据的全局JSON格式 1.自定义GsonHttpMessageConverter 2.自定义Gson对象 三、使用FastJson处理JSON (1)使用示例 (2) 修改特定数据的全局JSON格式 JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。JSON是一个标记符的序列。这套标记符包含六个构造字符、字符串、数字和三个字面名。JSON是一个序列化的对象或数组。 一、Spring Boot默认的JSON解析 Spring Boot项目中当我们添加了spring-boot-starter-web依赖,就默认提供了Jackson用于解析Json (1)使用示例 创建一个实体类 @Component public class User { private long id ; private String username ; private Date birthday ; public

Getting POJO objects into JTable

假装没事ソ 提交于 2020-01-30 12:06:23
问题 I'm trying to populate a JTable with POJOS that I've extracted using Gson(). Using the debugger, or by printing the values to the console using toString(), I can see that the mapping of the objects was successful. My problem lies with populating my custom Jtable with the objects. Problem: My GUI contains a button that takes a search field, and sends it to an API which returns a response in JSON, which I map correctly but I do not know how to get the data into the table. What I've tried : The

java.lang.illegalstateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY Retrofit

喜欢而已 提交于 2020-01-30 11:47:07
问题 i got error java.lang.illegalstateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 42 when Call API with retrofit, here is my code : Here is my interface : @FormUrlEncoded @POST("myCollection") Call<APIResponse<List<MyCollection>>> getMyCollection( @Field("caller_id") String caller_id ); Here is for Call : public class UploadVideoToneActivity extends BaseActivity{ List<MyCollection> collection = new ArrayList<>(); private void loadCollection() { ContactItem callerID =

java.lang.illegalstateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY Retrofit

送分小仙女□ 提交于 2020-01-30 11:46:27
问题 i got error java.lang.illegalstateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 42 when Call API with retrofit, here is my code : Here is my interface : @FormUrlEncoded @POST("myCollection") Call<APIResponse<List<MyCollection>>> getMyCollection( @Field("caller_id") String caller_id ); Here is for Call : public class UploadVideoToneActivity extends BaseActivity{ List<MyCollection> collection = new ArrayList<>(); private void loadCollection() { ContactItem callerID =

Proguard and Gson - Convert variable name inside arraylist

自古美人都是妖i 提交于 2020-01-30 10:59:48
问题 In debug, my code works fine. When I release my apk with proguard 5, Proguard offuscate my variables, and when I try to "Jsonify", the resulting string is offuscated, and so, my PHP script doesn't understand anything. Here's my code: ArrayList lCapturas=...; if (lCapturas != null) { Gson gson = new Gson(); Type listOfCapturaObject = new TypeToken<List<Captura>>() { }.getType(); json = gson.toJson(lCapturas, listOfCapturaObject); } and my Json string [{"j":"2014-09-10 17:35:25","e":"2014-09-

Moshi equivalent of Gson's “serializeNulls”

浪尽此生 提交于 2020-01-30 08:20:07
问题 I recently replaced Moshi for Gson in a backend that's expected to serialize responses with nullable values as { "value": null } , instead of {} . Neither Moshi nor Gson do this by default, but Gson has an option to do it directly in the builder: Gson gson = new GsonBuilder().serializeNulls().create() . Does Moshi have support for something similar? 回答1: You can call serializeNulls() on any JsonAdapter to get a JsonAdapter that'll serialize nulls. 来源: https://stackoverflow.com/questions

Serialize object using GSON

为君一笑 提交于 2020-01-30 06:23:10
问题 How can I serialize and deserialize this object with gson to json: public class test{ @Expose public List< Pair<Double,Double> > list; @Expose public int alpha; } I've tried this: Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); String str = gson.toJson(testInstance,test.class); where testInstance is an instance of class test, but it's not working because of Pair structure in List. 回答1: You have configured Gson object using excludeFieldsWithoutExposeAnnotation