I have the following string:
"[{\"type\":\"string\",\"value\":\"value1\",\"field\":\"label\"},
{\"type\":\"string\",\"value\":\"value2\",\"field\":\"address_unique\"}]"
and I want to turn this into java GSON JsonObject array/list in order to invoke something like:
myJsonObjs[i].getAsJsonPrimitive("value").getAsString();
What is the shortest path to achieve this? I tried setting up a bean like this:
public static class Filter {
@SerializedName("type")
private String type;
@SerializedName("value")
private String value;
@SerializedName("field")
private String field;
//getters, setters and ctor
}
but I cannot find a way to deserialize the string above into an array/list of this bean using JsonParser or Gson. In particular I tried
Gson gson = new Gson();
gson.fromJson(json_string, Filter[].class);
(where json_string is the string above) but this gives me a
IllegalStateException: Expected BEGIN_ARRAY but was STRING
Thank you for your help!
gson.fromJson(json_string, new TypeToken<List<Filter>>(){}.getType())?IllegalStateException: Expected BEGIN_ARRAY but was STRING...json_string.replace("\\","").replace("\"[", "[").replace("]\"","]");solved the problem... Thanks!