gson

how to parse JSON file with GSON

别来无恙 提交于 2019-12-17 07:19:49
问题 I have a very simple JSON with reviews for products, like: { "reviewerID": "A2XVJBSRI3SWDI", "asin": "0000031887", "reviewerName": "abigail", "helpful": [0, 0], "unixReviewTime": 1383523200, "reviewText": "Perfect red tutu for the price. ", "overall": 5.0, "reviewTime": "11 4, 2013", "summary": "Nice tutu" } { "reviewerID": "A2G0LNLN79Q6HR", "asin": "0000031887", "reviewerName": "aj_18 \"Aj_18\"", "helpful": [1, 1], "unixReviewTime": 1337990400, "reviewText": "This was a really cute",

Gson Java reserved keyword

杀马特。学长 韩版系。学妹 提交于 2019-12-17 06:46:08
问题 I have some JSON that I am deserializing using Gson. { "resp": { "posts": [ { ... "public": true, ... }] } My problem is that public is a Java keyword, so how would I make a field in my class that correlates with the public field in the JSON? 回答1: You could use a different name for your field, using gson's Field Naming Support. public class Post { @SerializedName("public") private boolean isPublic; ... } 回答2: Worth a quick note that you need to include gson.annotations or SerializedName for

GSON deserializing key-value to custom object

十年热恋 提交于 2019-12-17 06:41:23
问题 I need to deserialize json which is an array of date/long values. Here is an example of the returned JSON: [{"2011-04-30T00:00:00-07:00":100}, {"2011-04-29T00:00:00-07:00":200}] Using GSON I am able to deserialize this to a List<Map<Date,String>> , but would like to be able to convert it to a List<MyCustomClass> similar to: public class MyCustomClass() { Date date; Long value; } I cannot seem to find a way to instruct GSON to map the key/value of the JSON map to the date/value fields in my

Converting json string to java object?

青春壹個敷衍的年華 提交于 2019-12-17 06:38:21
问题 I have been looking around for examples related to converting JSON strings to Java object but haven't found any good examples. The one I found was really basic once and not really dealing with complex JSON strings. I am making an app to translate strings from english to different languages using google translate api. Google's response upon query is...foolowing text is formatted in JSON, {"data":{"translations":[{"translatedText":"Bonjour tout le monde"}]}} my approach so far is using GSON API

gson.toJson() throws StackOverflowError

大城市里の小女人 提交于 2019-12-17 04:31:23
问题 I would like to generate a JSON String from my object: Gson gson = new Gson(); String json = gson.toJson(item); Everytime I try to do this, I get this error: 14:46:40,236 ERROR [[BomItemToJSON]] Servlet.service() for servlet BomItemToJSON threw exception java.lang.StackOverflowError at com.google.gson.stream.JsonWriter.string(JsonWriter.java:473) at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:347) at com.google.gson.stream.JsonWriter.value(JsonWriter.java:440) at com

Could not serialize object cause of HibernateProxy

巧了我就是萌 提交于 2019-12-17 03:55:50
问题 I am getting the following error response from the server. HTTP Status 500 - type Exception report message descriptionThe server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter? root cause java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class:

How to parse dynamic JSON fields with GSON?

淺唱寂寞╮ 提交于 2019-12-17 03:38:12
问题 So I'm using GSON to parse JSON from an API and am stuck as to how to have it parse the dynamic fields in the data. Here is an example of the JSON data returned on a query: { - 30655845: { id: "30655845" name: "testdata description: "" latitude: "38" longitude: "-122" altitude: "0" thumbnailURL: http://someimage.com/url.jpg distance: 9566.6344386665 } - 28688744: { id: "28688744" name: "testdata2" description: "" latitude: "38" longitude: "-122" altitude: "0" thumbnailURL: http://someimage

Gson handle object or array

时光毁灭记忆、已成空白 提交于 2019-12-17 02:35:18
问题 I've got the following classes public class MyClass { private List<MyOtherClass> others; } public class MyOtherClass { private String name; } And I have JSON that may look like this { others: { name: "val" } } or this { others: [ { name: "val" }, { name: "val" } ] } I'd like to be able to use the same MyClass for both of these JSON formats. Is there a way to do this with Gson? 回答1: I came up with an answer. private static class MyOtherClassTypeAdapter implements JsonDeserializer<List

Jackson Vs. Gson [closed]

ぃ、小莉子 提交于 2019-12-16 22:34:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . After searching through some existing libraries for JSON, I have finally ended up with these two: Jackson Google GSon I am a bit partial towards GSON, but word on the net is that GSon suffers from a certain celestial performance issue (as of Sept 2009). I am continuing my

How to parse JSON Array (Not Json Object) in Android

萝らか妹 提交于 2019-12-16 19:51:57
问题 I have a trouble finding a way how to parse JSONArray. It looks like this: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...] I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects). But it's all I have and have to go with it. *EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out. There are a lot of examples out there, but they