Java – how to convert string arrays to objects using gson / JSON?

I have a JSON like this:

[
  [
    "Passport Number","NATIONALITY","REASONS"
  ],[
    "SHAIS100","INDIA",""
  ],[
    "","","Agent ID is not matched."
  ],""
  ]
]

I want to fill it into ArrayList < string [] >, please tell me what to do?

Empty string should not be converted to null

Solution

This is simple. You only need to do the following:

1. - first create the gson object:

Gson gson = new Gson();

2. - then get the address book type of your list < string [] > (please note that you cannot do this like list < string [] >. Class due to Java's type erasure):

Type type = new TypeToken<List<String[]>>() {}.getType();

3. - finally, resolve JSON to a structure of type:

List<String[]> yourList = gson.fromJson(yourJsonString,type);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>