如何根据 json 数组解析或创建 pojo?
How to parse, or create pojo from following json array?
我想解析以下 JSON 并使用我的代码(如字符串分词器)正常工作,但我想使用任何标准方法(例如 GSON.. 等)或如何为其创建 POJO 进行解析,using this,请问如何解决这个问题,谢谢
JSON
[
[
"106639",
"jonni",
"assistant director",
"1"
],
[
"106639",
"maikel",
"operator and publisher",
"1"
]
]
您没有自定义 json 对象。它是数组中的简单字符串数组,因此您应该使用 ArrayList> class 类型,如下所示。
科特林:
val model = Gson().fromJson(jsonString, ArrayList<ArrayList<String>>()::class.java)
Java:
ArrayList<ArrayList<String>> model = new Gson().fromJson(jsonString, new TypeToken<ArrayList<ArrayList<String>>>() {}.getType());
public class TypeToken<T> extends Object
Represents a generic type T.Java
doesn't yet provide a way to represent generic types, so this
class does. Forces clients to create a subclass of this class which
enables retrieval the type information even at runtime. For example,
to create a type literal for List<String>
, you can create an empty
anonymous inner class:
TypeToken<List<String>> list = new TypeToken<List<String>>() {};
This syntax cannot be used to create type literals that have wildcard
parameters, such as Class<?> or List<? extends CharSequence>
.
我想解析以下 JSON 并使用我的代码(如字符串分词器)正常工作,但我想使用任何标准方法(例如 GSON.. 等)或如何为其创建 POJO 进行解析,using this,请问如何解决这个问题,谢谢
JSON
[
[
"106639",
"jonni",
"assistant director",
"1"
],
[
"106639",
"maikel",
"operator and publisher",
"1"
]
]
您没有自定义 json 对象。它是数组中的简单字符串数组,因此您应该使用 ArrayList> class 类型,如下所示。
科特林:
val model = Gson().fromJson(jsonString, ArrayList<ArrayList<String>>()::class.java)
Java:
ArrayList<ArrayList<String>> model = new Gson().fromJson(jsonString, new TypeToken<ArrayList<ArrayList<String>>>() {}.getType());
public class TypeToken<T> extends Object
Represents a generic type
T.Java
doesn't yet provide a way to represent generic types, so this class does. Forces clients to create a subclass of this class which enables retrieval the type information even at runtime. For example, to create a type literal forList<String>
, you can create an empty anonymous inner class:
TypeToken<List<String>> list = new TypeToken<List<String>>() {};
This syntax cannot be used to create type literals that have wildcard parameters, such as
Class<?> or List<? extends CharSequence>
.