如何使用GSON / JSON将字符串数组转换为对象? [英] How to convert string array to object using GSON/ JSON?
本文介绍了如何使用GSON / JSON将字符串数组转换为对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的json:
I have a json like this:
[
[
"Passport Number",
"NATIONALITY",
"REASONS"
],
[
"SHAIS100",
"INDIA",
""
],
[
"",
"",
"Agent ID is not matched."
],
[
"",
"",
""
]
]
我想把它填充到 ArrayList< String []>
,请告诉我怎么办?
I want to populate this to ArrayList<String[]>
,Please tell me how to do?
空字符串不应该转换为空。
And empty strings should not convert as null.
推荐答案
1.-首先创建 Gson
object:
1.- First create the Gson
object:
Gson gson = new Gson();
2.-然后获取记者类型
( List< String []>
)(注意,由于Java的类型擦除): p>
2.- Then get the correspondent Type
for your List<String[]>
(Note that you can't do something like List<String[]>.class
due to Java's type erasure):
Type type = new TypeToken<List<String[]>>() {}.getType();
3.-最后将JSON解析为类型 code>:
3.- Finally parse the JSON into a structure of type type
:
List<String[]> yourList = gson.fromJson(yourJsonString, type);
这篇关于如何使用GSON / JSON将字符串数组转换为对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文