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