gson

How can i parse JSON from Webservice result with Gson

对着背影说爱祢 提交于 2019-12-13 04:15:37
问题 When i send request to server. I get result data with this format: { "menu": { "7": [{ "m_id": "1", "m_flag": "1", "m_type": "7", "m_name": "\u30cf\u30a4\u30cd\u30b1\u30f3", "m_price": "1000", "m_cost": "158", "m_regist_date": "0000-00-00", "p_id": "0" }, { "m_id": "2", "m_flag": "1", "m_type": "7", "m_name": "\u30ae\u30cd\u30b9", "m_price": "1000", "m_cost": "250", "m_regist_date": "0000-00-00", "p_id": "0" },.... "2": [{ "m_id": "149", "m_flag": "1", "m_type": "2", "m_name": "\u30da\u30fc

How to use fromGson with arrays

南楼画角 提交于 2019-12-13 04:06:29
问题 I have the json: {"test": [{"param1": "param"}], "test2": "test", "test3": "test2"} how can I get fromGson to parse the arrays in a json correctly so that it populates the arrays into an array of a particular object type. So test should map to TestObject[] test; test2 -> String test2; test3 -> String test3; I tried gson.fromJson(response, TestData.class); that gave me an error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line

Converting JSON list as different Java Objects in GSON

半世苍凉 提交于 2019-12-13 03:59:56
问题 I have the following json string: [ { "question" : { "questionId" : 1109, "courseId" : 419 }, "tags" : ["PPAP", "testtest"], "choices" : [{ "choiceId" : 0, "questionId" : 0 }, { "choiceId" : 0, "questionId" : 0 } ] } ] How do I make question, tags, and choices into separate objects using GSON? Currently I only use fromJson and can only convert a JSON string if it only contains 1 type of object. 回答1: You can have following classes class Question{ questionId; //specify data type courseId; }

Parsing JSON Array to Java List Using Gson

好久不见. 提交于 2019-12-13 03:58:25
问题 What is the best way from the given JSON to generate List of SimpleTestClass type where there's a new SimpleTestClass object for the values in the recipients array in the JSON with code set as well. public class SimpleTestClass{ String code; String recipient; } JSON payload: { "code": 123, "recipients": [ "888888", "222222" ] } 回答1: If JSON structure does not fit to POJO model you need to write custom deserialiser or create a new POJO model which fits JSON and after deserialisation process

colon (:) within JSON data using Gson

£可爱£侵袭症+ 提交于 2019-12-13 03:56:55
问题 I'm calling a web service that returns JSON. Within that JSON I have a property that holds a URL. But the colon (:) within that URL is making Gson throw a gson.stream.MalformedJsonException error. I know these keys and values should be wrapped JSON returned by web service: { ID=15; Code=ZPFgNr; UserName=https://www.google.com/accounts/o8/id?id=xxxxxx; //<--problem FirstName=Joe } My Java: resultData=((SoapObject) result).getProperty(0).toString(); User response = gson.fromJson(resultData,

Android and GSon

十年热恋 提交于 2019-12-13 03:54:44
问题 I am starting to look at google's Gson. The following code errors as it is entered in that the Gson in the last line is not recognised. The imports are ok so what am I missing? import com.google.gson.*; import com.google.gson.stream.*; public class BuildJsonObject { Gson myGson = new Gson(); } 回答1: Did you add the GSON Jar to your build path? Right Click (Project) -> Build Path -> Configure Build Path... -> Go To 'Libraries' Tab -> Click 'Add External JARs...' then select the GSON jar file?

GSON recursively decode map keys

主宰稳场 提交于 2019-12-13 03:44:32
问题 I want to use GSON to decode an array of maps in which the keys are not Strings. I know that the JSON type does not allow for objects to be used as keys, so I was hoping it is possible to have GSON work recursively to decode Strings. Java public class Reader { static class Key { int a; int b; } static class Data { HashMap<Key, Integer> map; } public static void read() { Gson gson = new Gson(); String x = "[{\"map\": { \"{\\\"a\\\": 0, \\\"b\\\": 0}\": 1 }}]"; Data[] y = gson.fromJson(x, Data[

ListView in the first activity, listView in the second activity; nested Json- no images in second listView.

人走茶凉 提交于 2019-12-13 03:35:23
问题 I’m using Gson and Universal Image Loader. I used this project like a template (you need change version to run it, it shows in tutorial): tutorial: https://www.youtube.com/watch?v=X2mY3zfAMfM link to this project: https://github.com/hishamMuneer/JsonParsingDemo I aded images in nested part of JSON and created listView in second activity to display it. { "movies": [ { "movie": "Avengers", "cast": [ { "name": "Robert Downey Jr.", "image": "www.mywebsite.com/img2.png" }, { "name": "Chris Evans",

Scala Object with ArrayBuffer - Deserialize issue using Gson

坚强是说给别人听的谎言 提交于 2019-12-13 03:09:20
问题 I have a top level scala class something like below: FinalOutput.scala: class FinalOutput extends Serializable { @BeanProperty var userId: String = _ @BeanProperty var tenantId: String = _ @BeanProperty @SerializedName("type") var dataType: String = _ @BeanProperty var data: FinalData = _ @BeanProperty var userCreatedDate: String = _ } FinalData.scala : class FinalData extends Serializable { @BeanProperty var list1: ArrayBuffer[DataType1] = _ @BeanProperty var list2: ArrayBuffer[DataType2] =

Gson and only concentrate on inner object

拥有回忆 提交于 2019-12-13 02:54:59
问题 Ok I have a json coming in that looks like this: {count:xxx, important:[{...}]} All I care about is the important object. Is there anything I can do with a custom deserializer or anything where I can just have GSON deserialize that important json object? Also the way I want the deserialized object to look is like this: List<Important> = Gson.fromJson(arg0, new TypeToken<ArrayList<Important>>(){}.getType() ); 回答1: Yes, you can use the JsonParser class from the Gson library to read it as a