net.sf.json.JSONArray与net.sf.json.JSONObject方法总结

您所在的位置:网站首页 jsonobject类有哪些方法 net.sf.json.JSONArray与net.sf.json.JSONObject方法总结

net.sf.json.JSONArray与net.sf.json.JSONObject方法总结

2024-07-11 14:24| 来源: 网络整理| 查看: 265

这段时间工作用到json比较多,于是系统的将net.sf.json.JSONArray与net.sf.json.JSONObject中的方法学习了一遍,下面贴出代码,其中用问号标注出来的就是暂时没有懂的地方(后面改进),有不足的地方还请指出谢谢,纯手写原创,可以任意转载但请标明出处。

import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Properties; import com.origin.entity.Index; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * 测试jsonObject的用法 * * @author utsc1243 * @date 2019年5月10日 */ public class TestJson { static Properties properties = new Properties(); public static void main(String[] args) { String str = testStr(); System.out.println(str); Map map = new HashMap(); map.put("name", "Woodie"); // 将对象转化成jsonObject的对象 JSONObject jsonObject = JSONObject.fromObject(str); // 根据key值从jsonObject中获取一个jsonObject对象 // JSONObject jsonData = jsonObject.getJSONObject("data"); JSONObject jsonData = jsonObject.optJSONObject("data"); // 根据key值从jsonObject中获取一个JSONArray的对象, getXXX没有对应的key会报错,optXXX返回默认值 // JSONArray jsonIndexArray = jsonData.getJSONArray("index"); JSONArray jsondaily = jsonData.optJSONArray("daily"); // 根据key值从jsonObject中获取一个String字符串对象 // String msg = jsonObject.getString("msg"); // optXXX与getXXX的区别:opt中调用的时候出现异常会返回 // Object object = jsonData.optString("city");默认值、 // discard与remove底层源码几乎是一样的,返回值不同, // discard是返回移除之后剩下的键值对,remove返回的是移除键值对中的值 // JSONObject removeObject = jsonObject.discard("data"); // Object removeObject = jsonObject.remove("data"); // 累计值;如果键存在那么值会叠加在后面 // jsonData.accumulate("city", "广州"); // element put 都是向json字符串中添加键值对 // 在源码中 put方法调用的是element来处理数据,put 比element多了一层校验 // jsonData.element("city", "广州"); // jsonData.put("city", true); // 清空json中的数据 // jsonData.clear(); // 判断jsonObject中是否有该键 // jsonData.has("date"); // 求json中的键值对的多少 // int jsonSize = jsonObject.size(); // 两个jsonObject对象进行比较,源码中比较二者的键值对的多少,返回-1、0、1 // int isMore = jsonObject.compareTo(jsonData); // 直接将一个键值对存放进入jsonobject对象中 // jsonObject.accumulateAll(map); // 判断jsonObject对象中是否存在key这个键 // 源码中都使用map中的 containsKey(key)方法,has中会使用verifyIsNull()进行校验,containsKey中不会进行校验 // jsonObject.containsKey("data"); // jsonObject.has("data"); // 判断jsonObject中是否有这个值,源码调用Map中的containsValue方法 // jsonData.containsValue("深圳"); // 将数据转换成set集合类型的数据 // jsonData.entrySet(); // 比较json数据是否相同 // jsonData.equals(jsonData); // 判断该json数据是否是数组类型的json数据 // jsonData.getJSONArray("index").isArray(); // 判断该json数据是否为空 // jsonData.clear(); // jsonData.isEmpty(); // ???不太明白 // jsonData.isNullObject(); // 迭代出json中的key // @SuppressWarnings("unchecked") // Iterator keys = jsonData.keys(); // while(keys.hasNext()){ // System.out.println(keys.next()); // } // 返回所有的key 的set集合 // @SuppressWarnings("unchecked") // Set keySet = jsonData.keySet(); // 返回返回的key的集合,集合中带引号,names源码中调用了keys()获取key, 然后通过element将数据添加到集合中去 // JSONArray names = jsonData.names(); // 传入names(JSONObject调用names方法获取到names的JSONArray) 返回返回键的value // 注:必须是jsonObject.ToJSONArray() 调用的参数必须是 jsonObject.names不能是其它的, // JSONArray values = jsonObject.toJSONArray(jsonObject.names()); // System.out.println(arr); // values()返回键值对中返回值的collection集合, 注意这里转化为arraylist的方法, 源码调用Map的values方法 // @SuppressWarnings("unchecked") // ArrayList arr = new ArrayList(jsonObject.values()); // for (int i = 0; i < arr.size(); i++) { // System.out.println(arr.get(i)); // } // ???????????????????????io中 writer中用法 // jsonObject.write(); // 将json转化为特定换类 // jsonObject = JSONObject.fromObject("{id:101,name:'mack',age:17,sex:'1'}"); // Student stu = (Student) JSONObject.toBean(jsonObject, Student.class); /* // 定义一个map集合存放class, 用于作为参数 Map classMap = new HashMap(); // 这里需要转化的参数一定需要与json字符串中的一致,例:"index" 不能是"indexs" Data类中也必须是index // 将转换使用到的类的class放入classMap中, 这里可以不将toBean(json, class, map)中的class放入map中去 classMap.put("data", Data.class); classMap.put("pm25", PM25.class); classMap.put("index", Index.class); classMap.put("daily", Daily.class); Data data = (Data) JSONObject.toBean(jsonData, Data.class, classMap); PM25 pm25 = data.getPm25(); ArrayList indexs = (ArrayList) data.getIndex(); */ // ------------------------------------------------------------------ // 操作JSONArray // 数组类型的json字符串 String jsonStr = jsondaily.toString(); System.out.println(jsonStr); // 将字符串转化为JSONOArray对象 // JSONArray daily = JSONArray.fromObject(jsonStr); // 计算维度,返回值与长度一样, // int [] dimension = JSONArray.getDimensions(daily); JSONArray indexs = jsonData.optJSONArray("index"); // 将jsonArray对象转化为指定的对象数组 // Index [] indexs = (Index[]) JSONArray.toArray(index, Index.class); // 将jsonArray对象转化为指定的对象集合 // @SuppressWarnings("unchecked") ArrayList indexList = (ArrayList) JSONArray.toCollection(indexs, Index.class); // 向JSONArray对象中快速添加一个相同的对象json数据 Index newIndex = new Index(); newIndex.setName("呵呵"); newIndex.setLevel("你想说什么"); newIndex.setMsg("建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。"); // 这里的不能是JSONArray.fromObject(newIndex),否则存入的是一个数组形式的数据 JSONObject json = JSONObject.fromObject(newIndex);//将java对象转换为json对象 // 添加数据,下面二者没有区别,element()方法中就是调用的add()方法 // indexs.add(json); // indexs.element(json); // 会将所有的indexList中的数据以json字符串的形式传入josnArray对象中 // indexs.addAll(indexList); // 清空目标json中的数据 // indexs.clear(); // 比较两个JSONArray的size的大小 , 时为1 // jsondaily.compareTo(indexs); // 传入一个对象,判断该jsonArray对象中是否有该对象转化而成的json数据 // indexs.contains(newIndex); // 传入一个集合,判断该jsonArray对象中是否有该 对象转化而成的json数据 // indexs.containsAll(indexList); // 根据下标移除其中的数据, // indexs.discard(0); // indexs.remove(0); // 移除一个对象,但是没有验证成功, 需要去学习List集合的remove(Object 0); // indexs.remove(newIndex); // 根据下标获取单个数据 // indexs.get(0); // 获取jsonArray中的第多少个,获取到返回的值是 // indexs.element(true); // indexs.getBoolean(indexs.size()-1); // 当前对象 的hashCode值 // indexs.hashCode(); // 传入一个对象,返回列表元素第一次出现的索引 // indexs.indexOf(true); // 传入一个对象没有达到预期效果 // indexs.add(newIndex); // 返回最后出现的索引, 源码调用的是List中的lastIndexOf()方法 // indexList.lastIndexOf(true); // 判断是否为数组 // indexs.isArray(); // 判断是否为空 // indexs.isEmpty(); // ???????? // indexs.isExpandElements(); // 返回此JSONArray的迭代器 // Iterator it = indexs.iterator(); // 返回此JSONArray的迭代器,源码中与iterator调用的是同一个方法 // Iterator it = indexs.listIterator(1); // 将字符串插入到各个元素*之间,最终返回字符串 // String indexsJoinStr = indexs.join("________"); // optXXX方法同 getXXX optXXX更加安全 // indexs.optString(0); // 移除jsonArray对象中所包含换collection集合 // indexs.addAll(jsondaily); // 将第几位设置为什么数据 // indexs.set(0, true); // 转化为字符串传入参数可以控制json的样式 // indexs.toString(1); // 给jsonArray中的数据加上一个名字 // String names = "[\"1\",\"2\",\"3\",\"4\",\"5\"]"; // JSONArray temp = JSONArray.fromObject(names); // JSONObject jObject = indexs.toJSONObject(temp); // ---------------------------------------------- } public static String testStr() { return "{\r\n" + " \"data\": {\r\n" + " \"city\": \"深圳\",\r\n" + " \"temphigh\": \"25\",\r\n" + " \"templow\": \"19\",\r\n" + " \"updatetime\": \"2017-11-04 13:23:00\",\r\n" + " \"tempnow\": \"24\",\r\n" + " \"sendibletemp\": \"27\",\r\n" + " \"winddirect\": \"东北风\",\r\n" + " \"windpower\": \"2级\",\r\n" + " \"humidity\": \"42\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"weather\": \"多云\",\r\n" + " \"week\": \"星期六\",\r\n" + " \"nl\": null,\r\n" + " \"date\": \"2017-11-04\",\r\n" + " \"index\": [\r\n" + " {\r\n" + " \"name\": \"化妆指数\",\r\n" + " \"level\": \"控油\",\r\n" + " \"msg\": \"建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"感冒指数\",\r\n" + " \"level\": \"易发\",\r\n" + " \"msg\": \"感冒容易发生,少去人群密集的场所有利于降低感冒的几率。\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"洗车指数\",\r\n" + " \"level\": \"不宜\",\r\n" + " \"msg\": \"雨(雪)水和泥水会弄脏您的爱车,不适宜清洗车辆。\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"穿衣指数\",\r\n" + " \"level\": \"舒适\",\r\n" + " \"msg\": \"白天温度适中,但早晚凉,易穿脱的便携外套很实用。\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"紫外线强度指数\",\r\n" + " \"level\": \"弱\",\r\n" + " \"msg\": \"辐射较弱,涂擦SPF12-15、PA+护肤品。\"\r\n" + " },\r\n" + " {\r\n" + " \"name\": \"运动指数\",\r\n" + " \"level\": \"不适宜\",\r\n" + " \"msg\": \"受到阵雨天气的影响,不宜在户外运动。\"\r\n" + " }\r\n" + " ],\r\n" + " \"pm25\": {\r\n" + " \"aqi\": 0,\r\n" + " \"co\": 8,\r\n" + " \"o3\": 42,\r\n" + " \"pm10\": 63,\r\n" + " \"pm2_5\": 64,\r\n" + " \"quality\": \"良\",\r\n" + " \"so2\": 4,\r\n" + " \"no2\": 11,\r\n" + " \"updatetime\": \"2017-11-04 13:00:00\"\r\n" + " },\r\n" + " \"daily\": [\r\n" + " {\r\n" + " \"date\": \"2017-11-04\",\r\n" + " \"week\": \"星期六\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"25\",\r\n" + " \"templow\": \"19\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-05\",\r\n" + " \"week\": \"星期日\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"26\",\r\n" + " \"templow\": \"19\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-06\",\r\n" + " \"week\": \"星期一\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"27\",\r\n" + " \"templow\": \"20\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-07\",\r\n" + " \"week\": \"星期二\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"21\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-08\",\r\n" + " \"week\": \"星期三\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"29\",\r\n" + " \"templow\": \"22\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-09\",\r\n" + " \"week\": \"星期四\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"22\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n" + " \"date\": \"2017-11-03\",\r\n" + " \"week\": \"星期五\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"18\",\r\n" + " \"weather\": \"晴\"\r\n" + " }\r\n" + " ]\r\n" + " },\r\n" + " \"status\": 0,\r\n" + " \"msg\": \"ok\"\r\n" + "}"; } }

需要用到的实体类Student

这里没有写getter and Setter请自己添加(后面的实体类也是如此)

import java.util.List; public class Student { Long id; String name; Integer age; String sex; }

需要用到的实体类PM25

package com.origin.entity; /** * * @author utsc1243 * @date 2019年5月20日 */ public class PM25 { Integer aqi; Integer co; Integer o3; Integer pm10; Integer pm2_5; String quality; Integer so2; Integer no2; String updatetime; }

需要的实体类Index

package com.origin.entity; /** * * @author utsc1243 * @date 2019年5月20日 */ public class Index { String name; String level; String msg; }

需要的实体类data

package com.origin.entity; import java.util.List; public class Data { String city; String temphigh; String templow; String updatetime; String tempnow; String sendibletemp; String winddirect; String windpower; String humidity; String sunrise; String sunset; String weather; String week; String nl; String date; List index; PM25 pm25; List daily; }

需要的实体类daily

package com.origin.entity; /** * * @author utsc1243 * @date 2019年5月20日 */ public class Daily { private String date; private String week; private String sunrise; private String sunset; private String temphigh; private String templow; private String weather; }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3