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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM