When parsing Json data, we encounter two-dimensional or more-dimensional array processing methods.
Json is as follows:
{"code": 1, "data": [ [ { "nodename": "test", "addr": "Beijing, China xx area xx road x Number", "uid": "123", } ], [ { "nodename": "test", "addr": "Beijing, China xx area xx road x Number", "uid": "123", } ], [ { "nodename": "test", "addr": "Beijing, China xx area xx road x Number", "uid": "123", } ] ], "message": "success!"}
Seeing this kind of JSON, the new comrades will surely feel overwhelmed, as if it is troublesome!?
JsonObject
Using native JsonObject, the JsonArray approach can be very cumbersome. First, parse JsonObject== to parse "data" JsonArray=> and then parse JsonArray [i]=> and then JsonObject.
Does it seem obtuse and complicated?
There must be a simpler way to parse this multidimensional JSON array.
Gson
Today we will talk about Gson's parsing multidimensional arrays:
- Using Gson to parse json data, we will definitely use entity classes to create our UserBean entity classes based on json content
Keep in mind that Serializable serialization interfaces must be implemented so that they can be parsed or converted properly.
public class UserBean implements Serializable { private String addr; private String nodename; private String uid; public String getUid() { return userid; } public void setUid(String uid) { this.uid = uid; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getNodename() { return nodename; } }
2. Analyse the Json structure to be resolved:
{"code": 1, "data": [ [ { "nodename": "test", "addr": "Beijing, China xx area xx road x Number", "uid": "123", } ] ], "message": "success!"}
Analytical steps: First, the most peripheral key pairs as parameter names in entity classes must be consistent with the key names in json data. When we generate entity classes, the general parameter names are lowercase.
(2) The type of the key-value pair corresponding to the value is the type of the parameter name in our entity class. Extension:
For example, "data": [] When this key-value pair is encountered, the parameters in the entity class can be written as List < Object > data; thus, the subarrays in the entity class can be parsed.
Back to the point, if you look closely at our Json, you will find that the value of "data" is a two-dimensional array, and the data in the second dimension is the data we need, that is, our UserBean. Simply, the root entity for direct parsing is OK:
class MyDataBean implements Serializable { private String code; private List<List<UserBean>> data; private String message; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<List<UserBean>> getData() { return data; } public void setData(List<List<UserBean>> data) { this.data = data; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } }
The final parsing is done in two lines of code:
Gson gson = new Gson(); MyDataBean myDataBean = gson.fromJson(jsonstr, MyDataBean.class); //In this way, two-dimensional arrays are completely imported into our entity classes. To get the value, you just need to go through it: UserBeanList = new ArrayList<UserBean>(); for (int i = 0; i < myDataBean.getData().size(); i++) { List<List<UserBean>> mlslsAmb = myDataBean.getData(); if (mlslsAmb != null && mlslsAmb.size() > 0) { UserBean abean = mlslsAmb.get(i).get(0); UserBeanList.add(abean); } }