gson

RuntimeException in Gson parsing JSON: Failed to invoke protected java.lang.ClassLoader() with no args

℡╲_俬逩灬. 提交于 2019-12-09 17:35:09
问题 I've inherited some code that saves our application state as JSON using Gson, and then reads it using fromJson. Gson gson = createGson(); gson.fromJson(objString, myClass); One of the fields being saved is a Location. Unfortunately, very occasionally the parsing of that saved data fails because my saved Location includes an mClassLoader in its mExtras, and the Gson library fails to create the ClassLoader with this error: RuntimeException: Failed to invoke protected java.lang.ClassLoader()

Install Gson from Github

老子叫甜甜 提交于 2019-12-09 16:50:30
问题 I'm just starting to learn Java and I need Gson for a new project that I'm working on. I feel like I'm missing something with all the installation instructions that I find online--all of them refer to three jar files that I should be able to extract from a zip file. But when I look at Gson on github i can't find any .jar files! https://github.com/google/gson Once I find the jar files, I'm just supposed to include them in the buildpath for the project I'm working on right? I'm working on this

Android技能-创建Json文本及Json解析

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 16:25:49
摘要:Json数据在安卓开发过程中是非常常见的,在请求服务器端数据的时候,服务器端返回的无非就是三种类型:HTML,XML,JSON。所以学习JSON对安卓程序员来说是非常重要的。 什么是JSON JSON:JavaScript Object Notation。顾名思义,JSON数据是源自于JavaScript,学习过JavaScript(以下简称JS)的人都知道,我们在JS脚本里面创建对象时,都是以键值对的形式编写的。例如,我们在JS里面创建一个Json对象时是这么定义的: var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; 很容易看出,属性之间是通过逗号分隔开,键与值之间是通过双引号分隔开,而JSON数据也是如此。 JSON的数据类型 json一共有两种数据结构。 JsonObject 一种是以 (key/value)对形式存在的无序的jsonObject对象。对象使用大括号括起来,属性之间使用逗号分隔开。 例如:{"name": "jack"}, 这就是一个最简单的json对象,对于这种数据格式,key值必须要是string类型,而对于value,则可以是string、number、object、array等数据类型。 JsonArray 另一种数据格式就是有序的value的集合

File or Database? - Best Practice to save Objects on Android-Device

喜欢而已 提交于 2019-12-09 15:41:58
问题 I'm building an android application in java where I define some Objects like "user" or "playlist" etc.. How to save these self-defined objects on the device for later access? Gson gson = new Gson(); String json = gson.toJson(user); I can parse the objects via GSON to a JSONObject or a JSONArray. Now I have two options to save the strings: In a Database or a File. I know how to use the android database-classes and the filewriter/reader classes, but what is the best practice with regards to

Deserializing json array using gson?

南笙酒味 提交于 2019-12-09 13:15:49
问题 I have my json as: { "status": 200, "data": [ { "catId": 638, "catName": "Helena Bonham Carter", "catUniqueName": "helena-bonham-carter", "catSlug": "" }, { ... } ] } My Category model as: public class Category { private double catId; private String catName; private String catUniqueName; private String catSlug; } And my gson custom deserializer is as follows: Type listType = new TypeToken<ArrayList<Category>>(){}.getType(); Gson gson = new GsonBuilder() .registerTypeAdapter(listType, new

Gson Serializing HashMap<Teacher, List<Student>>

自作多情 提交于 2019-12-09 12:17:00
问题 I have a map where the a key's value is a list of objects. Am able to serialize the keys through builder.enableComplexMapKeySerialization(); but the values are not serialized as expected because they return a string on deserialization instead of object. Below is the output of serialization [{"id":31001,"name":Teacher"]}, //This is the key [{"id":33033,"name":"student1"}, {"id":34001,"name":"student2"}]], //This is the list of values I used the relevant TypeToken which is TypeToken<HashMap

Gson fromJson deserialize generics

ぐ巨炮叔叔 提交于 2019-12-09 11:47:28
问题 While working on an Android app I've faced a problem when I wanted to make an ultimate generic methods to send HTTP-requests (with loopj) and deserialize them (with Gson). As you may know while using gson.fromJson you cannot do like this: gson.fromJson(responseBody, new TypeToken<T>() {}.getType()); or gson.fromJson(responseBody, new TypeToken<ArrayList<T>>() {}.getType()) Instead of actual object (or list of actual objects in 2nd case) that you've passed as T , you will get LinkedTreeMap

JPA2/Hibernate - Stop lazy loading?

一个人想着一个人 提交于 2019-12-09 10:51:02
问题 I'm having a problem where JPA is trying to lazily load my data when I don't want it to. Essentially what is happening is I'm using a Service to retrieve some data, and when I go to parse that data into JSON, the JSON library is triggering hibernate to try and lazily load the data. Is there any way to stop this? I've given an example below. // Web Controller method public String getEmployeesByQuery(String query) { Gson gson = new Gson(); List<Employee> employees = employeeService

Gson deserialize interface to its Class implementation

[亡魂溺海] 提交于 2019-12-09 09:44:49
问题 I am using Retrofit 2.1.0 with converter-gson:2.1.0 and separately gson:2.6.2 in order to customize the serialization/deserialization. The problem is that my POJOs should be hidden behind interfaces and I want to tell Gson which class should be the deserialized interface. And after the deserialization/ serialization Retrofit should be able to return the interface. It would be good if I can take advantage of Generics and easily create a way to tell Gson or Retrofit to serialize/deserialize

What is the easiest way to parse json using gson when the element to parse is an element of a json string?

非 Y 不嫁゛ 提交于 2019-12-09 08:46:09
问题 I am using gson to parse json into java beans. For the API I am using, a large number of the json results include the result as the first property of a json object. The "gson way" seems to be to create an equivalent wrapper java object, which has one property for the target output type - but this results in unnecessary throwaway classes. Is there a best practice way of doing this? For example to parse: {"profile":{"username":"nickstreet","first_name":"Nick","last_name":"Street"}} I have to do

工具导航Map