gson

Gson序列化时@SerializedName的使用

不问归期 提交于 2020-01-07 16:20:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Gson是java中比较常见的json序列化工具,实习参与的项目由于最开始是C#开发的.net项目,近两年才开始用java,但之前的很多项目仍然在使用C#并且还在更新。于是牵涉到跨语言的程序通信, 公司自己用thrift封装了一个服务路由实现。但两个语言有些不一样的地方挺蛋疼,比如命名规则,java喜欢首字母小写,C#喜欢首字母大写,传数据就需要特殊处理一下,不过Gson自带@SerializedName注解已经可以基本解决问题。 今天敲代码遇到个问题,有个类的某个成员变量(也是个类)转出来所有属性都是null,其他都正常,检查了半天发现就是因为那个成员变量的类用了@SerializedName注解,而其他成员变量没有,而且那里所用json字符串也是小写。。。 做个笔记,Gson的@SerializedName注解使用。 没用注解时: public class User { private String name; private String slogan; private Integer age; public User(String name, Integer age, String slogan) { this.name = name; this.age = age; this.slogan =

GSON does not deserialize reference to outer class

浪尽此生 提交于 2020-01-07 05:29:05
问题 On my Java application, I defined two classes, called A and B where B is inner class of A . Both are defined as serializable public class A implements Serializable { int attrParent; List<B> items = new ArrayList<B>(); public void setAttrParent(int attrParent) { this.attrParent = attrParent; } public int getAttrParent() { return attrParent; } public class B implements Serializable { private int attr; public void setAttr(int attr) { this.attr = attr; } public int getAttr() { return attr; }

Gson().fromJson(jsonResult, Myobject.class) return values in 0's

拟墨画扇 提交于 2020-01-07 03:08:35
问题 I have this json {"FechaUpdateId":1,"FechaSegundos":635902782941643690,"FechaSegundosBC":635902779020935030} I execute this command for convert it to object FechaUpdate dto = new Gson().fromJson(json, FechaUpdate.class); But I got this result this is my FechaUpdate class public class FechaUpdate { public int getFechaUpdateId() { return fechaUpdateId; } public void setFechaUpdateId(int fechaUpdateId) { this.fechaUpdateId = fechaUpdateId; } public long getFechaSegundosBC() { return

Gson().fromJson(jsonResult, Myobject.class) return values in 0's

三世轮回 提交于 2020-01-07 03:08:28
问题 I have this json {"FechaUpdateId":1,"FechaSegundos":635902782941643690,"FechaSegundosBC":635902779020935030} I execute this command for convert it to object FechaUpdate dto = new Gson().fromJson(json, FechaUpdate.class); But I got this result this is my FechaUpdate class public class FechaUpdate { public int getFechaUpdateId() { return fechaUpdateId; } public void setFechaUpdateId(int fechaUpdateId) { this.fechaUpdateId = fechaUpdateId; } public long getFechaSegundosBC() { return

java.lang.StackOverflowError converting HttpServletRequest to JSON

心已入冬 提交于 2020-01-07 03:04:52
问题 In my spring rest app, I log every api endpoints arguments in aspect. @Aspect @Component public class EndpointsAspect { @Around("execution(@org.springframework.web.bind.annotation.RequestMapping * *(..))") public Object handle(ProceedingJoinPoint joinPoint) throws Throwable { Map<String, Object> log = new HashMap<>(); String[] parameterNames = methodSignature.getParameterNames(); Object[] parameterValues = joinPoint.getArgs(); Map<String, Object> arguments = new HashMap<>(); for (int i = 0; i

How to use gson and volley parse nested json?

蹲街弑〆低调 提交于 2020-01-07 02:54:30
问题 Here is the json data I want to use: { "name" : "Ravi Tamada", "email" : "ravi8x@gmail.com", "phone" : { "home" : "08947 000000", "mobile" : "9999999999" } } Here is my JsonObjectRequest: JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, APITEST,null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Gson gson = new Gson(); People people; people = gson.fromJson(jsonObject.toString(),People.class); tv_city.setText(""

parsing array in gson in java

不羁的心 提交于 2020-01-07 02:44:28
问题 i am getting following data from php script.. {"errorno":"fe_200","message":"successful","univ":[{"univ_id":"1","univ_name":"abc","slug":"uptu","thumb":"http:\/\/www.fs.com\/notes\/img\/uptu.gif","table_prefix":"uptu"},{"univ_id":"2","univ_name":"def","slug":"gtu","thumb":"http:\/\/www.fs.com\/notes\/img\/gtu.gif","table_prefix":"gtu"}, {"univ_id":"3","univ_name":"jvc university","slug":"jntu","thumb":"http:\/\/www.fs.com\/notes\/img\/jntu.gif","table_prefix":"jntu"} }]} i want to parse this

Get Type of ParameterizedType from generic

旧时模样 提交于 2020-01-07 02:28:06
问题 I cannot understand fully how reflect works, so I'm having some trouble trying to register a custom type adapter for my GsonBuilder . I need to register a type adapter for a List<Something> type. This is a working code: gsonBuilder.registerTypeAdapter( new TypeToken<List<MyClass>>() { }.getType(), new ListDeserializer(MyClass.class) ); The thing is that I have a lot of classes to register my type adapter, and I have a ListDeserializer and SingleDeserializer adapters. I'm trying to go generic

GSON deserialize to bundle

ぐ巨炮叔叔 提交于 2020-01-07 01:50:08
问题 How can i deserialize this JSON: "data": { "key1": 11, "key2": { "key3": 22 "key4": 83 "key5": 99 } } to an Android Bundle using GSON library? This is not working: class Model implements Parcelable { private int key1; private Bundle key2; [...] protected Model(Parcel in) { key1 = in.readInt(); [...] key2 = in.readBundle(); } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(key1); [...] dest.writeBundle(key2); } } I don't want to create a key2 model. 回答1: ok, I can write an

How to deserialise .NET JsonResult DateTime format with Gson in Android?

孤街醉人 提交于 2020-01-07 00:07:09
问题 I have a JSON returned from server in this format: { "StartDate": "/Date(1451593800000)/", "EndDate": "/Date(1483216200000)/" } And in my POJO: public class Data { java.util.Date startDate; java.util.Date endDate; } And my Gson object: private final Gson gson = new GsonBuilder() .serializeNulls() .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .create(); When I wanted to parse this JSON I get this error: Unparseable date: "/Date(1483216200000)/" (at offset 0) What is the right way