How do I decode JSON by using GSON with this example?
I'm newbie with GSON. This is my example
[["a","b","c"],[1,2,3],[4,5,6]]
I want to split into array. I try this code
String test = "[ [\"a\",\"b\",\"c\"],[1,2,3]]";
String[] column 开发者_如何学C= gson.fromJson(test, String[].class);
It not works... and get error.
The JsonDeserializer StringTypeAdapter failed to deserialized json object ["a","b","c"] given the type class java.lang.String
com.google.gson.JsonParseException: The JsonDeserializer StringTypeAdapter failed to deserialized json object ["a","b","c"] given the type class java.lang.String
I know this example its mix of String array and Integer arrays.
How I deal with this ??
Thank you..
The problem is that you're trying to convert the JSON into an array of Strings, when in fact it's not an array of strings at all.
Since the array elements don't have property names, you can't create a normal Java class to store this either. Thus it's going to have to be deserialized as some sort of 2D array. Given that some elements are Strings, and some are Integers, the most specific common superclass is Object
.
Thus the only array class that this could possibly work as is Object[][].class
. I don't know if GSON supports heterogeneous 2D arrays, but try this and find out:
String[] column = gson.fromJson(test, Object[][].class);
If this fails to resolve the object, try passing in List.class
instead. This will probably fail as it gives GSON no type information with which to resolve the nested entities, but it's worth a try.
精彩评论