JSONObject 和JSONArray基本使用

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);
}

posted @ 2019-06-24 17:36  jason47  阅读(3286)  评论(0编辑  收藏  举报