gson

Json key with “.” as separator

荒凉一梦 提交于 2021-01-27 17:31:32
问题 I am using JSON to save settings and load them again after a restart. But now I want to use keys like "category.subcategory.variable" to get the value. I imagine something like boolean foo = json.get("category.subcategory.foo"); String bar = json.get("category.bar"); json.set("category.subcategory.baz", baz); and the json looks like this { category: { subcategory: { foo: false, baz: ["baz"] }, bar: "bar" } } I'm also using Gson, maybe it is possible there, but if it is only possible with

Gson: serialize transient field?

时间秒杀一切 提交于 2021-01-27 07:52:39
问题 Is there anyway to serialize transient field of a class? The documentation mentioned it is not supported by default but there might be a way to turn it on? Many thanks 回答1: I'm not a Gson expert, but I think you can add the annotation @com.google.gson.annotations.Expose javadocs here - note that you have to do a few other things too Alternatively, you can call GsonBuilder.excludeFieldsWithModifiers(int... modifiers) and leave out the transient flag. Search on "Java Modifier Excleusion" (or

Java object with variable-dimensional array

梦想与她 提交于 2021-01-27 07:40:50
问题 I'm trying to parse a GeoJSON using Gson, and I have an JSON that looks something like this (simplified): {"type": "FeatureCollection", "features": [ { "type": "Feature", "name": "Afghanistan", "geometry": { "type": "Polygon", "coordinates": <<a 3-dimensional double array (double[][][] in Java)>> } }, { "type": "Feature", "name": "Indonesia", "geometry": { "type": "MultiPolygon", "coordinates": <<a 4-dimensional double array (double[][][][] in Java)>> } }, //etc... ] } I need to have a java

Gson property order in android

天涯浪子 提交于 2021-01-27 07:11:18
问题 I have integrated Gson to create the json used in a request for an android application. Here is my model class public class TwitterUser { @Expose public String gid; public String icon_url; public Boolean is_app_user; @Expose public String displayName; public TwitterUser(String l, String i, String url, Boolean app_user) { gid = i; displayName = l; icon_url = url; is_app_user = app_user; } public TwitterUser(String l, String i) { gid = i; displayName = l; } public String getGid() { return gid;

Moshi - Parse unknown json keys

一个人想着一个人 提交于 2021-01-27 05:13:09
问题 How can I parse with moshi a json structure that has keys that are unknown at compile time: "foo": { "name": "hello", "bar": { "unknownKey1": { "a": "1" } }, "unknownKey2": { "b": "2" }, "unknownKeyX": { "c": "X" } }, "properties": {...} } I tried using a @FromJson adapter for JSONObject but the logs just say that the json is empty {} (where I would expect {"unknownKey1": { ... etc ...} ) class Foo { @Json(name = "name") String name; @Json(name = "bar") Bar bar; static class Bar { } } class

Android Retrofit Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

孤街醉人 提交于 2021-01-26 19:33:16
问题 I am using okhttp Retrofit in my Android App to make network requests. On one of the requests I get this error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ I see a 201 response in the logs but Retrofit throws this error. Below is my code. signup(signupParams, new Callback<Member>() { @Override public void success(Member member, Response response) { if (member != null) { UserAccount userAccount = new

Android Retrofit Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

為{幸葍}努か 提交于 2021-01-26 19:29:24
问题 I am using okhttp Retrofit in my Android App to make network requests. On one of the requests I get this error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ I see a 201 response in the logs but Retrofit throws this error. Below is my code. signup(signupParams, new Callback<Member>() { @Override public void success(Member member, Response response) { if (member != null) { UserAccount userAccount = new

Android Retrofit Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

萝らか妹 提交于 2021-01-26 19:28:16
问题 I am using okhttp Retrofit in my Android App to make network requests. On one of the requests I get this error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ I see a 201 response in the logs but Retrofit throws this error. Below is my code. signup(signupParams, new Callback<Member>() { @Override public void success(Member member, Response response) { if (member != null) { UserAccount userAccount = new

Android Retrofit Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

[亡魂溺海] 提交于 2021-01-26 19:26:08
问题 I am using okhttp Retrofit in my Android App to make network requests. On one of the requests I get this error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ I see a 201 response in the logs but Retrofit throws this error. Below is my code. signup(signupParams, new Callback<Member>() { @Override public void success(Member member, Response response) { if (member != null) { UserAccount userAccount = new

how to deserialize a json string that contains @@ with scala'

一曲冷凌霜 提交于 2021-01-20 08:59:37
问题 As the title already explains, I would like to deserialize a json string that contains a key that starts with @@. With the @@ my standard approach using case classes sadly does not work anymore. val test = """{"@@key": "value"}""" case class Test(@@key: String) // not possible val gson = new GsonBuilder().create() val res = gson.fromJson(test, classOf[Test]) How can work with the @@ withtout preprocessing the input json string? 回答1: The simplest answer is to quote the field name: case class