gson

GSON - JsonSyntaxException - Expected name at line 7 column 4

末鹿安然 提交于 2020-01-22 17:50:06
问题 I have the following result class whose object is to be returned as JSON. public class Result { public String objectid; public String dtype; public String type; public String name; public String description; public Result() { this.objectid = ""; this.dtype= ""; this.type=""; this.name= ""; this.description = ""; } public String getObjectid() { return objectid; } public void setObjectid(String s) { this.objectid = s; } public String getDtype() { return dtype; } public void setDtype(String s) {

Auto-value-gson with an interface error, register an InstanceCreator?

∥☆過路亽.° 提交于 2020-01-22 02:46:05
问题 I have an interface class that looks like this. public interface Species { String name(); } And a Human class that implements @AutoValue with a TypeAdapter. @AutoValue public abstract class Human implements Parcelable, Species { public static Human create(String humanVariable) { return new AutoValue_Human(humanVariable); } public static Human create(String name, String humanVariable) { return new AutoValue_Human(name, humanVariable); } public static TypeAdapter<Human> typeAdapter(Gson gson) {

Retrofit returns LinkedTreeMap instead of JSON

故事扮演 提交于 2020-01-21 14:57:49
问题 I am using Retrofit 2.0 to read data from a web service which returns a json text, but in following code response is LinkedTreeMap and I can't convert it to json: My interface public interface GeoAPIInterface { String ENDPOINT = "http://www.geoplugin.net/"; @GET("json.gp") Call<Object> getIP(@Query("ip") String ip); } Now I read data from site : Retrofit retrofit = new Retrofit.Builder() .baseUrl(GeoAPIInterface.ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) .build();

Java to Json validation using GSON

帅比萌擦擦* 提交于 2020-01-21 05:25:08
问题 While converting Java object to Json string using GSON API, I also want to fail this Json conversion if any of the annotated attribute is null. For example public class Order{ @SerializedName("orderId") @Expose @Required private Integer id; //getter & setter available for id } Now as I am doing Order order = new Order(); JSONObject jsonobj = new JSONObject(gson.toJson(order)); I want to fail the above Java to Json transformation if any of the @Required attribute is null Is this possible using

JAVA Json 转 DTO 报错:Expected a string but was BEGIN_OBJECT

痞子三分冷 提交于 2020-01-21 04:41:24
JAVA Json 转 DTO 报错:Expected a string but was BEGIN_OBJECT at line 1 column 352 UserDTO user = gson . fromJson ( dataJson , UserDTO . class ) ; 由于DTO 中有date类型的变量,所以需要在builder指定格式。如: Gson gson = ( new GsonBuilder ( ) ) . setDateFormat ( "dd/MM/yyyy HH:mm:ss" ) . create ( ) ; UserDTO user = gson . fromJson ( dataJson , UserDTO . class ) ; 来源: CSDN 作者: LuoJianXing 链接: https://blog.csdn.net/qq_32894331/article/details/104054089

How do I install the GSON module in Java

跟風遠走 提交于 2020-01-20 21:46:07
问题 I downloaded the Google JSON module a.k.a GSON. I'm ona windows system Could you tell me how to install the GSON module? I extracted the JAR into the following folder which was in my classpath: C:\Program Files\Java\jdk1.6.0_07\lib ..but when i type: import com.google.gson.Gson; import com.google.gson.GsonBuilder; I still get a module not found error. What am I doing wrong? Thanks. 回答1: You should not extract the JAR. You should not put 3rd party libraries in JDK/lib . You need to specify it

Create object from Gson string doesn't work

丶灬走出姿态 提交于 2020-01-20 05:37:08
问题 I want to pass an object to a bundle. Now I did the conversion to a Json object with GSON. The bundle is passed, the bundle has the right string representation but when I create the object from the string I get a huge error. FATAL EXCEPTION: main java.lang.RuntimeException: Failed to invoke private android.net.Uri() with no args at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:107) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter

josn-lib,jackson,gson与fastjson的区别

こ雲淡風輕ζ 提交于 2020-01-19 19:05:59
一 、各个JSON技术的简介和优劣 1.json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,包括commons-beanutils.jar,commons-collections.jar,commons-lang.jar,commons-logging.jar,ezmorph-1.0.6.jar,对于复杂类型的转换,json-lib对于json转换成bean还有缺陷,比如一个类里面会出现另一个类的list或者map集合,json-lib从json到bean的转换就会出现问题。 json-lib在功能和性能上面都不能满足现在互联网化的需求。 2.开源的Jackson 相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。Jackson对于复杂类型的json转换bean会出现问题,一些集合Map,List的转换出现问题。Jackson对于复杂类型的bean转换Json,转换的json格式不是标准的Json格式。 3.Google的Gson Gson是目前功能最全的Json解析神器,Gson当初是应Google公司内部需求而由Google自行研发而来的,但自从在2008年五月公开发布第一版后已被许多公司或用户应用

Howto deserialize field that is passed either as a single Object or as a list of Objects with GSON?

Deadly 提交于 2020-01-19 01:51:10
问题 I have successfully used GSON to convert a JSON to a single Object and to convert JSON to a list of objects. My problem is that there are 2 sources emitting data to me. One is only sending one object and the other is sending a list of Objects. Single object from 1st source: { id : '1', title: 'sample title', .... } List of objects from 2nd source: [ { id : '1', title: 'sample title', .... }, { id : '2', title: 'sample title', .... }, ... ] The class being used for deserializing: public class

JSON的学习与使用

自古美人都是妖i 提交于 2020-01-18 23:48:17
/*--> */ /*--> */ /*--> */ /*--> */ 公众号:小李不秃,Java 原创博主 阅读本文大概需要 7.8 分钟 前言 什么是 JSON 为什么有 JSON 如何使用 JSON JSON 的数据结构 Json 在 javaScript 中的使用 操作 Json 对象 操作 JSON 数组 JSON.parse() JSON.stringify() eval Json 在 Java 中的使用 Json-lib Gson Jackson fastJson 总结 参考 推荐阅读 前言 我们在进行软件开发的过程中,服务与服务之间会进行相互的调用。在数据进行传输前,我们通常会将数据转化成 JSON 的格式进行传输,比如 ajax 调用请求,接收传过来的 JSON 数据,javascript 就可以对传过来的数据进行直接的调用。 本篇文章会讲解以下的内容: 什么是 JSON---what 为什么有 JSON---why 如何使用 JSON---How JSON 的数据结构 Json 在 javascript 中的使用 Json 在 Java 中的使用 什么是 JSON JSON 全拼 Java Script Object Notation,JavaScript 对象表示法。是一种由道格拉斯·克罗克福特构想和设计、轻量级的 数据交换语言