gson

How to iterate a JSONObject (gson)

别等时光非礼了梦想. 提交于 2019-12-21 09:16:32
问题 I have a JsonObject e.g JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"} Every JSONObject contains a "id" entry, but th number of other key/value pairs is NOT determined, so I want to create create an object with 2 attributes: class myGenericObject { Map<String, Object> attributes; String id; } So I want my attributes map to look like this: "keyInt" -> 4711 "keyStr" -> "val1" I found this solution Map<String, Object> attributes = new HashMap<String, Object>(); Set<Entry

How can I get Gson to use accessors rather than fields?

妖精的绣舞 提交于 2019-12-21 07:47:18
问题 By default Gson uses fields as a basis for it's serialization. Is there a way to get it to use accessors instead? 回答1: The developers of Gson say that they never felt swayed by the requests to add this feature and they were worried about murkying up the api to add support for this. One way of adding this functionality is by using a TypeAdapter (I apologize for the gnarly code but this demonstrates the principle): import java.io.IOException; import java.lang.annotation.Retention; import java

Parsing JSON maps / dictionaries with Gson?

半腔热情 提交于 2019-12-21 07:28:51
问题 I need to parse a JSON Response that looks like: {"key1": "value1", "key2": "value2", "key3": {"childKey1": "childValue1", "childKey2": "childValue2", "childKey3": "childValue3" } } class Egg { @SerializedName("key1") private String mKey1; @SerializedName("key2") private String mKey2; @SerializedName("key3") // ??? } I'm reading through the Gson docs but cannot figure out how to properly deserialize a dictionary to a Map. 回答1: Gson readily handles deserialization of a JSON object with name

Gson force use int instead of double

夙愿已清 提交于 2019-12-21 07:28:12
问题 Hi can i configure gson that he use int instead of double got data like: {fn: chat, data: {roomId: 1, text: "Some brabble"}} I got a PacketClass to deserialize it to: public class DataPacket { private String fn; private Object p; // will be a StringMap } and decode like: DataPacket pkg = gson.fromJson(message, DataPacket.class); for better typehandling i got a data pojo for the DataPacket.p field for example: public class ChatPacket { public int roomId; public String message; public String

How to invoke another serializer from a custom Gson JsonSerializer?

为君一笑 提交于 2019-12-21 07:18:08
问题 I have my custom class User : class User { public String name; public int id; public Address address; public Timestamp created; } I'm now creating a custom JsonSerializer for User.class: @Override public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); obj.addProperty("name", src.name); obj.addProperty("id", src.id); obj.addProperty("address", src.address.id); // Want to invoke another JsonSerializer (TimestampAdapter) for

get key names from JSON object using Gson

扶醉桌前 提交于 2019-12-21 05:57:26
问题 I have a JSON object that I'd like to get the key names from and store them in an ArrayList. I've used the following code jsonData(String filename) { JsonParser parser = new JsonParser(); JsonElement jsonElement = null; try { jsonElement = parser.parse(new FileReader(filename)); } catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } JsonObject jsonObject = jsonElement.getAsJsonObject(); int i = 0; for (Entry<String

How to parse following JSON using GSON in android?

旧城冷巷雨未停 提交于 2019-12-21 05:36:26
问题 As keysArray (A,B,C) are dynamic and cores having the same object names ? Please help !!! { "result":"success", "keysArray":[ "A", "B", "C", "D", "E" ], "cores":{ "A":[{"key":"value"}], "B":[{"key":"value"}], "C":[{"key":"value"}], "D":[{"key":"value"}], "E":[{"key":"value"}] } } 回答1: I have achieved it in this way. String responseStr = //JsonResponse here Type type = new TypeToken<GetMainResponse>() {}.getType(); GetMainResponse getMainResponse = new GsonBuilder().excludeFieldsWithModifiers

Retrofit parse JSON dynamic keys

我的梦境 提交于 2019-12-21 05:02:20
问题 I'm a newbie in Retrofit. How to parse the Json below using retrofit? { "data": { "Aatrox": { "id": 266, "title": "a Espada Darkin", "name": "Aatrox", "key": "Aatrox" }, "Thresh": { "id": 412, "title": "o Guardião das Correntes", "name": "Thresh", "key": "Thresh" } }, "type":"champion", "version":"6.23.1" } 回答1: You could make your model POJO contain a Map<String, Champion> to deserialize into, to deal with the dynamic keys. Example: public class ChampionData { public Map<String, Champion>

Using Gson to parse Json array and object with no name

与世无争的帅哥 提交于 2019-12-21 04:36:08
问题 I know there are many JSON with GSON questions but none of them relate to me directly. My JSON is formatted differently. I have a JSON data I want to parse using GSON which looks like the following: [ { "foo":"1", "bar":[ { "_id":"bar1"} ], "too":["mall", "park"] } ] And I have the model Classes: ItemArray Class public class ItemArray { List<Item> itemArray; //Get set here } Item Class public class Item { String foo; List<Bar> bar; List<String> too; //Get set here } Bar Class public class Bar

Ignore null fields when DEserializing JSON with Gson or Jackson

孤街浪徒 提交于 2019-12-21 04:22:35
问题 I know there's lots of questions about skipping fields with a null value when serializing objects to JSON. I want to skip / ignore fields with null values when deserializing JSON to an object. Consider the class public class User { Long id = 42L; String name = "John"; } and the JSON string {"id":1,"name":null} When doing User user = gson.fromJson(json, User.class) I want user.id to be '1' and user.name to be 'John'. Is this possible with either Gson or Jackson in a general fashion (without