gson

Android Json Parsing with Gson

两盒软妹~` 提交于 2019-12-13 15:05:12
问题 I am tring to map json data using Gson from a URL to my java class. This is the code for the class: public class Sessions { public Boolean active; public String contributor_covu_id; public String created_at; public String key; public String status; public Boolean getActive() { return active; } public void setActive(Boolean active) { this.active = active; } public String getContributor_covu_id() { return contributor_covu_id; } public void setContributor_covu_id(String contributor_covu_id) {

Gson, auto-initialize and avoid null exceptions

陌路散爱 提交于 2019-12-13 13:29:20
问题 when deserializing a json into a class Foo{int id; List<String> items; List<Long> dates;} How could I auto initialize fields that are null after deserialization. Is there such a possiblity with Gson lib? ex: Foo foo = new Gson().fromJson("{\"id\":\"test\", \"items\":[1234, 1235, 1336]}", Foo.class) foo.dates.size(); -> 0 and not null pointerException I know I could do if (foo.attr == null) foo.attr = ... but I'm looking for more generic code, without knowledge of Foo class thx edit : sorry

json data storage with versioning

瘦欲@ 提交于 2019-12-13 12:33:51
问题 Problem definition: There is a Java server which stores JSON data that can be mapped to a Java Class. The Java Class is subject to changes. The goal is to be able to update the Java Class and still be able to decode the JSON data of an older version to a Java Object of a newer version. There should be a good versioning system in place. For example the ability to add a new field to the Java Class with a default value. In the case the old JSON data doesn't have this field, the Java object can

Gson - how to parse dynamic JSON string with nested JSON?

空扰寡人 提交于 2019-12-13 12:12:51
问题 I have JSON string with dynamic elements, till now I parse it into Map: Map map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, String>>() {}.getType()); Now I need to solve thsi situation - one of these dynamic variables could be another JSON string. Do you have some advice ho to solve it? Thanks in advance. EDIT: JSON string example added (formatted): { "key1": "val1", "key2": "val2", "key3": { "subkey1": [ "subvalue1", "subvalue1" ], "subkey‌​2": [ "subvalue2" ] }, "key4":

How to search for values inside Json file

独自空忆成欢 提交于 2019-12-13 11:06:48
问题 I have this json below and i need to get the value of "ID" which is "SMGL". i'm using Gson to save the json from file. how can I search for this? { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [

GSON - convert a string into a JsonArray

廉价感情. 提交于 2019-12-13 10:56:12
问题 I am trying to convert a string into a JsonArray. So far I have tried to do the following: Gson().toJson(string) Gson().toJsonTree(string) both throw an exception saying that the argument is not a JsonArray. Here is the string, as you can see it is a JsonArray: "[{\"match\":{\"id\":92757102,\"tournament_id\":3666234,\"state\":\"open\",\"player1_id\":58602461,\"player2_id\":58602459,\"player1_prereq_match_id\":null,\"player2_prereq_match_id\":null,\"player1_is_prereq_match_loser\":false,\

how to fix this error java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [duplicate]

瘦欲@ 提交于 2019-12-13 10:19:23
问题 This question already has answers here : Why does Gson fromJson throw a JsonSyntaxException: Expected BEGIN_OBJECT but was BEGIN_ARRAY? (2 answers) Closed 3 years ago . I'm implementing a restful client using Android. I have API URL, token, etc. I implement this application using Retrofit 2 library, all are correct. But the JSON values do not display properly. I've tried several ways to solved this problem but can't find any correct solution for this. This is my code: JSON string {"deposits":

Parse JSON objects(lat and lng) in GoogleMap as markers

本秂侑毒 提交于 2019-12-13 10:05:42
问题 When I parse the lat and lng in System.out.println(shop.getShopLat()); works fine. And the Map without marker works fine. Now I want to add several marker using shop.getShopLat() and shop.getShopLng(). But when I tried to implement this getting redflags: The constructor LatLng(ArrayList, ArrayList) is undefined Someone please help to solve this? 回答1: Please have a look at this line: for(Shop shop : this.response.shops){ map.addMarker(new MarkerOptions().position(new LatLng(shop.getShopLat(),

Parsing a long JSON using GSON

≡放荡痞女 提交于 2019-12-13 08:59:03
问题 I'm trying to parse a very long JSON like this. { "BlastOutput2": [ { "report": { "program": "blastn", "version": "BLASTN 2.6.0+", "reference": "Stephen F. Altschul, Thomas L. Madden, Alejandro A. Schäffer, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), \"Gapped BLAST and PSI-BLAST: a new generation of protein database search programs\", Nucleic Acids Res. 25:3389-3402.", "search_target": { "subjects": [ "Subject_1" ] }, "params": { "expect": 0.0001, "sc_match": 1, "sc

Gson ClassCastException (LinkedTreeMap)

天大地大妈咪最大 提交于 2019-12-13 08:44:00
问题 Using Gson to deserialize objects of a parameterized class Container<T> , results in the java.lang.ClassCastException for certain values of T , e.g. for a simple Record type consisting of String and List<String> fields: com.google.gson.internal.LinkedTreeMap cannot be cast to Record Interestingly enough, the same code works when calling the fromJson() method inline, i.e. the following statement does return a valid value: Container<Record> value = new Gson().fromJson( json, new TypeToken