I have a json like this:
[
[
"Passport Number",
"NATIONALITY",
"REASONS"
],
[
"SHAIS100",
"INDIA",
""
],
[
"",
"",
"Agent ID is not matched."
],
[
"",
"",
""
]
]
I want to populate this to ArrayList<String[]>
,Please tell me how to do?
And empty strings should not convert as null.
Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.
Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
Gson is a Java library that can be used to convert Java objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.
GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.
That's very simple, you just need to do the following:
1.- First create the Gson
object:
Gson gson = new Gson();
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.- Finally parse the JSON into a structure of type type
:
List<String[]> yourList = gson.fromJson(yourJsonString, type);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With