1,Gson 解析map
json数据格式为:{“Ipc0”:{"Address":"192.168.1.12","Password":"admin","UserName":"admin","Port":554},
"Ipc1":{"Address":"192.168.1.12","Password":"admin","UserName":"admin","Port":554},
"Ipc2":{"Address":"192.168.1.12","Password":"admin","UserName":"admin","Port":554}}
将数据解析为map
private Map
Gson gson = new Gson();
Map
if(isNotEmpty(jsonStr)){
Type type = new TypeToken
map = gson.fromJson(jsonStr,type);
}
return map;
}
2,解析数组转List
json数据格式为:{“found”:2,"records":[{"Channel":0,"CreateTime":1481597517,"RecNo":1,"Method":"one"},{"Channel":0,"CreateTime":1481597517,"RecNo":1,"Method":"two"}]}
private
List
try{
if (isNotEmpty(jsonStr)) {
JSONObject record = new JSONObject (jsonStr);
JSONArray recordArray = record.getJSONArray("records");
list = jsonToArrayList(recordArray.toString(),Record.class);
}
}catch(JSONException e){
e.printStachTrace();
}
return list;
}
private
Type type = new TypeToken
ArrayList
ArrayList
for (JsonObject jsonObject : jsonObjects){
arrayList.add(new Gson().fromJson(jsonObject ,clazz));
}
return arrayList;
}