gson

JavaWeb中使用JSON

强颜欢笑 提交于 2020-01-05 06:27:12
前言: 最近也是期末了,有好多好多文档和实验报告要交,所以都没啥时间写文,这段时间清闲了,来补一下之前学习时遗漏的一些知识树,话说就没人吐槽这个JSON图标好丑吗? 什么是JSON JSON 指的是 JavaScript 对象表示法( J ava S cript O bject N otation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSON 具有自我描述性,更易理解 JSON 使用 JavaScript 语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 这里有意思的是,JSON本来是用来表示 JavaScript 对象的一种数据文本格式,但由于它轻量级、易于解析/操作(JavaScript原生支持)的一些特点,渐渐的被很多语言支持也就成了一种标准 为什么使用JSON 在JSON之前,我们通常在网络传输中使用的格式是XML,在我们的印象之中,XML具有很好的可读性,并且格式统一,解析起来也相对比较简单,为什么摒弃掉XML而逐渐的使用起JSON呢? 主要原因在于: JSON比XML更小、更快、更易解析。 JavaScript原生支持JSON,解析速度相较XML会更快; XML解析成DOM对象的时候,浏览器之间会产生差异【例如IE和FireFox】;

Android: convert Parcelable to JSON

∥☆過路亽.° 提交于 2020-01-05 03:39:06
问题 I am working with socket.io library which emits messages to the socket.io server. The server expects JSON objects, arrays, etc. My original implementation used JSONOject and JSONArray datatypes. However, I would like to switch to using classes generated via Parceler library. The classes generated with library's annotations can be wrapped into Parcels. It seems like a very convenient way of managing such communication. However, is there a way to convert Parceler's class or a Parcel class into

Android Retrofit + GSON - Enum serialization for API calls - Annoying extra quotation marks added

最后都变了- 提交于 2020-01-05 03:07:31
问题 This problem (this might be by design, who knows - not simple to find a clear answer, at any rate) occurs in both Android Retrofit 1.9 and 2.0, and with the latest GSON library. I have an Enum class like such: public enum SomethingType { @SerializedName("first") First, @SerializedName("second") Second } And using Retrofit (2.0, but same issue with 1.9) along with GSON, I attempt to output the enum instance as such: @Multipart @POST("things") Call<ThingResponse> createThing( @Part("image")

Using gson to parse json to java object

你。 提交于 2020-01-04 06:17:47
问题 I want to parse a json data into a java object, then post it to a .jsp to then turn it back into a json object so I can put it into a javascript array. Here is an example of the data I want to parse: "LoanList" = [ {"Loan" : "One","Lat" : "35.65365", "Lng" : "123.1331" , "Bal" : "4565" , "Address" : "Address 1" , "Delinquency" : "True')] After reading about it, I decided to use gson to parse the data: After reading other questions about this topic, I wrote two classes. class1.java import java

How to parse nested JSON with GSON

不打扰是莪最后的温柔 提交于 2020-01-04 06:04:20
问题 Lets say i have a JSON like: { "assignments": [ { 'id': '111', 'activities': [ { 'activity': 'Activity 1', }, { 'activity': 'Activity 2' } ] }, { 'id': '2222', 'Activities': [ { 'activity': 'Activity 1' } ] } ] } And I'm using GSON to parse it. I have a correctly set up Javabean and can access id without problems. How do i get the activities per id / object? EDIT: more code: public class Assignment { private String id; public String getId() { return id; } } Gson mGson= new Gson();

How to parse nested JSON with GSON

ぃ、小莉子 提交于 2020-01-04 06:03:44
问题 Lets say i have a JSON like: { "assignments": [ { 'id': '111', 'activities': [ { 'activity': 'Activity 1', }, { 'activity': 'Activity 2' } ] }, { 'id': '2222', 'Activities': [ { 'activity': 'Activity 1' } ] } ] } And I'm using GSON to parse it. I have a correctly set up Javabean and can access id without problems. How do i get the activities per id / object? EDIT: more code: public class Assignment { private String id; public String getId() { return id; } } Gson mGson= new Gson();

Type Information within serialized JSON using GSON

独自空忆成欢 提交于 2020-01-04 05:55:19
问题 Using Jackson we have the option to attach the type info to the serialized object (http://jackson.codehaus.org/1.5.5/javadoc/org/codehaus/jackson/annotate/JsonTypeInfo.html). Is there a way to do this using GSON like: { propertyName:"test", _className:"foo.bar.TestClass" } The idea is to have it generic, so when a ArrayList<Object> is deserialized, the right object instances are restored with it. I saw this question: https://stackoverflow.com/a/8683689/1001027 that is more or less what I need

Using Retrofit and RxJava, how do I deserialize JSON when it doesn't map directly to a model object?

怎甘沉沦 提交于 2020-01-04 05:41:16
问题 The API I'm working with returns objects (and their containing objects) in a "flat" format and I'm having trouble getting this to work elegantly with Retrofit and RxJava. Consider this JSON response for an /employees/{id} endpoint: { "id": "123", "id_to_name": { "123" : "John Doe" }, "id_to_age": { "123" : 30 } } Using Retrofit and RxJava, how do I deserialize this to a Employee object with fields for name and age ? Ideally I'd like RxJava's onNext method to be called with an Employee object.

RETROFIT how to parse this response

痴心易碎 提交于 2020-01-04 05:31:08
问题 I'm trying to parse this json response yahoo yql with Retrofit,but the problem is that the response begins(as you can see in the link above) with the following characters: "finance_charts_json_callback(". Therefore i get the following error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ . Is posible to parse this json file with Retrofit? Thanks in advance 回答1: It returns a json that's invoked by a callback

Android and GSON throwing No-Args Constructor not found exception

百般思念 提交于 2020-01-04 05:20:21
问题 I am having a problem with GSON in my Android app. JSON Input [ {"id":"1202","parent_id":"0","agrpcount":"0","disp_order":"1","code":"PROD-NEW","name":"New Products","active":"1"}, {"id":"1119","parent_id":"0","agrpcount":"0","disp_order":"2","code":"PROD-VID","name":"\\\"How To\\\" Videos","active":"1"}, {"id":"1685","parent_id":"0","agrpcount":"0","disp_order":"3","code":"SUPP-RESOUR","name":"Support Resources","active":"1"}, {"id":"49","parent_id":"0","agrpcount":"0","disp_order":"109",