1. 打包
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "200");
jsonObject.put("message", "成功");
JSONArray deviceTypeArray = new JSONArray();
List list =产生一个List;
for(Object o:list) {
JSONObject dtJson = new JSONObject();
dtJson.put("name", (Integer)o);
deviceTypeArray.add(dtJson);
}
jsonObject.put(“data”,deviceTypeArray);
return jsonObject.toJSONString();
2.解析
String s = 返回一个String;
JSONObject jo = JSONObject.parseObject(s);
String ss = jo.getString("message");
System.out.println(ss);
JSONArray ja = jo.getJSONArray("data");
for(Object j:ja) {
Integer type = ((JSONObject) j).getInteger("name");
System.out.println("-------->"+type);
}