[
[
"hello",1,[2]
],[
"world",3,[2]
]
]
所以,这是一个数组,包含2个数组. 2个内部数组本身是由String,int,数组类型组成的数组.
我不确定如何使用java类来建立具有3种不同类型(String,array)的数组.我从头开始:
// String json just contains the aforementioned json string.
ArrayList<ArrayList<XXX>> data = new ArrayList<ArrayList<XXX>>();
Type arrayListType = new Typetoken<ArrayList<ArrayList<XXX>>>(){}.getType();
data = gson.fromJson(json,arrayListType);
但是,XXX应该在哪里?我认为它应该是一个数组,但它应该是一个具有3种不同数据类型的数组.那么我该如何用Java来建模呢?
可以帮忙吗
谢谢.
解决方法
当然,不需要将单组件阵列反序列化为非数组类型.例如,前面的例子可以反序列化为int [] data = gson.fromJson(“[3]”,int [].
当询问时,Gson还会将非String值反序列化为String.将这个应用到第一个例子,String data = gson.fromJson(“[3]”,String.class);工作也是如此.
请注意,告诉Gson将第一个示例反序列化为Object类型不起作用. Object data = gson.fromJson(“[3]”,Object.class);导致一个解析异常抱怨[3]不是一个原始的.
适用于上述原始问题的示例,如果将所有值视为字符串是可以接受的,则反序列化变得简单.
// output:
// hello 1 2
// world 3 2
public class Foo
{
static String jsonInput =
"[" +
"[\"hello\",[2]]," +
"[\"world\",[2]]" +
"]";
public static void main(String[] args)
{
Gson gson = new Gson();
String[][] data = gson.fromJson(jsonInput,String[][].class);
for (String[] data2 : data)
{
for (String data3 : data2)
{
System.out.print(data3);
System.out.print(" ");
}
System.out.println();
}
}
}
不幸的是,使用Gson,我无法找出一种简单的反序列化方法,可以在数组中更好地绑定到更具体和混合的类型,因为Java不提供定义混合类型数组的语法.例如,原始问题中的集合的首选类型可能是List< List< String,List< int>>,但是不可能在Java中定义.所以,你必须满足List< List< String>> (或String [] []),或者转到使用更多“手动”解析的方法.
(是的,Java允许List< Object>>的类型声明,但是Object并不是有意义地反序列化的特定的类型.而且,如所讨论的,尝试反序列化[3]到Object导致解析异常. )
小更新:我最近不得不反驳一些粗糙的JSON,包括一个与原来的问题不太相似的结构.我最终只是使用自定义解串器从混乱的JSON数组创建一个对象.类似于以下示例.
// output:
// [{MyThreeThings: first=hello,second=1,third=[2]},// {MyThreeThings: first=world,second=3,third=[4,5]}]
import java.lang.reflect.Type;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class FooToo
{
static String jsonInput =
"[" +
"[\"hello\"," +
"[\"world\",[4,5]]" +
"]";
public static void main(String[] args)
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyThreeThings.class,new MyThreeThingsDeserializer());
Gson gson = gsonBuilder.create();
MyThreeThings[] things = gson.fromJson(jsonInput,MyThreeThings[].class);
System.out.println(Arrays.toString(things));
}
}
class MyThreeThings
{
String first;
int second;
int[] third;
MyThreeThings(String first,int second,int[] third)
{
this.first = first;
this.second = second;
this.third = third;
}
@Override
public String toString()
{
return String.format(
"{MyThreeThings: first=%s,second=%d,third=%s}",first,second,Arrays.toString(third));
}
}
class MyThreeThingsDeserializer implements JsonDeserializer<MyThreeThings>
{
@Override
public MyThreeThings deserialize(JsonElement json,Type typeOfT,JsonDeserializationContext context)
throws JsonParseException
{
JsonArray jsonArray = json.getAsJsonArray();
String first = jsonArray.get(0).getAsstring();
int second = jsonArray.get(1).getAsInt();
JsonArray jsonArray2 = jsonArray.get(2).getAsJsonArray();
int length = jsonArray2.size();
int[] third = new int[length];
for (int i = 0; i < length; i++)
{
int n = jsonArray2.get(i).getAsInt();
third[i] = n;
}
return new MyThreeThings(first,third);
}
}
Gson用户指南涵盖了混合类型集合的反序列化,与the “Serializing and Deserializing Collection with Objects of Arbitrary Types” section类似.