net.sf.json.JSONArray与net.sf.json.JSONObject⽅法总结这段时间⼯作⽤到json⽐较多,于是系统的将net.sf.json.JSONArray与net.sf.json.中的⽅法学习了⼀遍,下⾯贴出代码,其中⽤问号标注出来的就是暂时没有懂的地⽅(后⾯改进),有不⾜的地⽅还请指出谢谢,纯⼿写原创,可以任意转载但请标明出处。
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.Map;
4import java.util.Properties;
5
ity.Index;
7
8import net.sf.json.JSONArray;
9import net.sf.json.JSONObject;
10
11/**
12 * 测试jsonObject的⽤法
13 *
14 * @author utsc1243
15 * @date 2019年5⽉10⽇
16 */
17public class TestJson {
18 static Properties properties = new Properties();
19
20 public static void main(String[] args) {
21 String str = testStr();
22 System.out.println(str);
23 Map<String,String> map = new HashMap<String, String>();
24 map.put("name", "Woodie");
25
26 // 将对象转化成jsonObject的对象
27 JSONObject jsonObject = JSONObject.fromObject(str);
28
29 // 根据key值从jsonObject中获取⼀个jsonObject对象
30 // JSONObject jsonData = JSONObject("data");
31 JSONObject jsonData = jsonObject.optJSONObject("data");
32
33 // 根据key值从jsonObject中获取⼀个JSONArray的对象, getXXX没有对应的key会报错,optXXX返回默认值
34 // JSONArray jsonIndexArray = JSONArray("index");
35 JSONArray jsondaily = jsonData.optJSONArray("daily");
36
37 // 根据key值从jsonObject中获取⼀个String字符串对象
38 // String msg = String("msg");
39 // optXXX与getXXX的区别:opt中调⽤的时候出现异常会返回
40 // Object object = jsonData.optString("city");默认值、
41
42 // discard与remove底层源码⼏乎是⼀样的,返回值不同,
43 // discard是返回移除之后剩下的键值对,remove返回的是移除键值对中的值
44 // JSONObject removeObject = jsonObject.discard("data");
45 // Object removeObject = ve("data");
46
47 // 累计值;如果键存在那么值会叠加在后⾯
48 // jsonData.accumulate("city", "⼴州");
49
50 // element put 都是向json字符串中添加键值对
51 // 在源码中 put⽅法调⽤的是element来处理数据,put ⽐element多了⼀层校验
52 // jsonData.element("city", "⼴州");
53 // jsonData.put("city", true);
54
55 // 清空json中的数据
56 // jsonData.clear();
57
58 // 判断jsonObject中是否有该键
59 // jsonData.has("date");
59 // jsonData.has("date");
60
61 // 求json中的键值对的多少
62 // int jsonSize = jsonObject.size();
63
64 // 两个jsonObject对象进⾏⽐较,源码中⽐较⼆者的键值对的多少,返回-1、0、1
65 // int isMore = jsonObjectpareTo(jsonData);
66
67 // 直接将⼀个键值对存放进⼊jsonobject对象中
68 // jsonObject.accumulateAll(map);
69
70
71 // 判断jsonObject对象中是否存在key这个键
72 // 源码中都使⽤map中的 containsKey(key)⽅法,has中会使⽤verifyIsNull()进⾏校验,containsKey中不会进⾏校验
73 // ainsKey("data");
74 // jsonObject.has("data");
75
76 // 判断jsonObject中是否有这个值,源码调⽤Map中的containsValue⽅法
77 // ainsValue("深圳");
78
79 // 将数据转换成set集合类型的数据
80 // Set();
81
82 // ⽐较json数据是否相同
83 // jsonData.equals(jsonData);
84
85 // 判断该json数据是否是数组类型的json数据
86 // JSONArray("index").isArray();
87
88 // 判断该json数据是否为空
89 // jsonData.clear();
90 // jsonData.isEmpty();
91
92 // 不太明⽩
93 // jsonData.isNullObject();
94
95 // 迭代出json中的key
96 // @SuppressWarnings("unchecked")
97 // Iterator<String> keys = jsonData.keys();
98 // while(keys.hasNext()){
99 // System.out.());
100 // }
101
102 // 返回所有的key 的set集合
103 // @SuppressWarnings("unchecked")
object to104 // Set<String> keySet = jsonData.keySet();
105 // 返回返回的key的集合,集合中带引号,names源码中调⽤了keys()获取key, 然后通过element将数据添加到集合中去106 // JSONArray names = jsonData.names();
107 // 传⼊names(JSONObject调⽤names⽅法获取到names的JSONArray) 返回返回键的value
108 // 注:必须是jsonObject.ToJSONArray() 调⽤的参数必须是 jsonObject.names不能是其它的,
109 // JSONArray values = JSONArray(jsonObject.names());
110 // System.out.println(arr);
111
112 // values()返回键值对中返回值的collection集合,注意这⾥转化为arraylist的⽅法,源码调⽤Map的values⽅法
113 // @SuppressWarnings("unchecked")
114 // ArrayList<JSONObject> arr = new ArrayList<JSONObject>(jsonObject.values());
115 // for (int i = 0; i < arr.size(); i++) {
116 // System.out.(i));
117 // }
118
119 // io中 writer中⽤法
120 // jsonObject.write();
121
122 // 将json转化为特定换类
123 // jsonObject = JSONObject.fromObject("{id:101,name:'mack',age:17,sex:'1'}");
124 // Student stu = (Student) Bean(jsonObject, Student.class);
124 // Student stu = (Student) Bean(jsonObject, Student.class);
125
126 /*
127 // 定义⼀个map集合存放class, ⽤于作为参数
128 Map<String, Object> classMap = new HashMap<String, Object>();
129 // 这⾥需要转化的参数⼀定需要与json字符串中的⼀致,例:"index" 不能是"indexs" Data类中也必须是index 130 // 将转换使⽤到的类的class放⼊classMap中,这⾥可以不将toBean(json, class, map)中的class放⼊map中去131 classMap.put("data", Data.class);
132 classMap.put("pm25", PM25.class);
133 classMap.put("index", Index.class);
134 classMap.put("daily", Daily.class);
135 Data data = (Data) Bean(jsonData, Data.class, classMap);
136 PM25 pm25 = Pm25();
137 ArrayList<Index> indexs = (ArrayList<Index>) Index();
138 */
139
140 // ------------------------------------------------------------------
141 // 操作JSONArray
142 // 数组类型的json字符串
143 String jsonStr = String();
144 System.out.println(jsonStr);
145
146 // 将字符串转化为JSONOArray对象
147 // JSONArray daily = JSONArray.fromObject(jsonStr);
148
149 // 计算维度,返回值与长度⼀样,
150 // int [] dimension = Dimensions(daily);
151
152 JSONArray indexs = jsonData.optJSONArray("index");
153 // 将jsonArray对象转化为指定的对象数组
154 // Index [] indexs = (Index[]) Array(index, Index.class);
155 // 将jsonArray对象转化为指定的对象集合
156 // @SuppressWarnings("unchecked")
157 ArrayList<Index> indexList = (ArrayList<Index>) Collection(indexs, Index.class);
158
159 // 向JSONArray对象中快速添加⼀个相同的对象json数据
160 Index newIndex = new Index();
161 newIndex.setName("呵呵");
162 newIndex.setLevel("你想说什么");
163 newIndex.setMsg("建议⽤露质⾯霜打底,⽔质⽆油粉底霜,透明粉饼,粉质胭脂。");
164 // 这⾥的不能是JSONArray.fromObject(newIndex),否则存⼊的是⼀个数组形式的数据
165 JSONObject json = JSONObject.fromObject(newIndex);//将java对象转换为json对象
166 // 添加数据,下⾯⼆者没有区别,element()⽅法中就是调⽤的add()⽅法
167 // indexs.add(json);
168 // indexs.element(json);
169
170 // 会将所有的indexList中的数据以json字符串的形式传⼊josnArray对象中
171 // indexs.addAll(indexList);
172
173 // 清空⽬标json中的数据
174 // indexs.clear();
175
176 // ⽐较两个JSONArray的size的⼤⼩ , <= 时返回 -1 内容相等时为0 > 时为1
177 // jsondailypareTo(indexs);
178
179 // 传⼊⼀个对象,判断该jsonArray对象中是否有该对象转化⽽成的json数据
180 // ains(newIndex);
181 // 传⼊⼀个集合,判断该jsonArray对象中是否有该对象转化⽽成的json数据
182 // ainsAll(indexList);
183
184 // 根据下标移除其中的数据,
185 // indexs.discard(0);
186 // ve(0);
187 // 移除⼀个对象,但是没有验证成功,需要去学习List集合的remove(Object 0);
188 // ve(newIndex);
189
189
190 // 根据下标获取单个数据
191 // (0);
192
193
194 // 获取jsonArray中的第多少个,获取到返回的值是
195 // indexs.element(true);
196 // Boolean(indexs.size()-1);
197
198 // 当前对象的hashCode值
199 // indexs.hashCode();
200
201 // 传⼊⼀个对象,返回列表元素第⼀次出现的索引
202 // indexs.indexOf(true);
203 // 传⼊⼀个对象没有达到预期效果
204 // indexs.add(newIndex);
205 // 返回最后出现的索引, 源码调⽤的是List中的lastIndexOf()⽅法
206 // indexList.lastIndexOf(true);
207
208 // 判断是否为数组
209 // indexs.isArray();
210
211 // 判断是否为空
212 // indexs.isEmpty();
213
214 //
215 // indexs.isExpandElements();
216
217 // 返回此JSONArray的迭代器
218 // Iterator<JSONObject> it = indexs.iterator();
219 // 返回此JSONArray的迭代器,源码中与iterator调⽤的是同⼀个⽅法
220 // Iterator<JSONObject> it = indexs.listIterator(1);
221
222 // 将字符串插⼊到各个元素*之间,最终返回字符串
223 // String indexsJoinStr = indexs.join("________");
224
225 // optXXX⽅法同 getXXX optXXX更加安全
226 // indexs.optString(0);
227
228 // 移除jsonArray对象中所包含换collection集合
229 // indexs.addAll(jsondaily);
230
231 // 将第⼏位设置为什么数据
232 // indexs.set(0, true);
233
234 // 转化为字符串传⼊参数可以控制json的样式
235 // String(1);
236
237 // 给jsonArray中的数据加上⼀个名字
238 // String names = "[\"1\",\"2\",\"3\",\"4\",\"5\"]";
239 // JSONArray temp = JSONArray.fromObject(names);
240 // JSONObject jObject = JSONObject(temp);
241
242 // ----------------------------------------------
243
244 }
245
246 public static String testStr() {
247 return "{\r\n" + " \"data\": {\r\n" + " \"city\": \"深圳\",\r\n" + " \"temphigh\": \"25\",\r\n" 248 + " \"templow\": \"19\",\r\n" + " \"updatetime\": \"2017-11-04 13:23:00\",\r\n"
249 + " \"tempnow\": \"24\",\r\n" + " \"sendibletemp\": \"27\",\r\n"
250 + " \"winddirect\": \"东北风\",\r\n" + " \"windpower\": \"2级\",\r\n"
251 + " \"humidity\": \"42\",\r\n" + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n" 252 + " \"weather\": \"多云\",\r\n" + " \"week\": \"星期六\",\r\n" + " \"nl\": null,\r\n"
253 + " \"date\": \"2017-11-04\",\r\n" + " \"index\": [\r\n" + " {\r\n"
254 + " \"name\": \"化妆指数\",\r\n" + " \"level\": \"控油\",\r\n"
254 + " \"name\": \"化妆指数\",\r\n" + " \"level\": \"控油\",\r\n"
255 + " \"msg\": \"建议⽤露质⾯霜打底,⽔质⽆油粉底霜,透明粉饼,粉质胭脂。\"\r\n" + " },\r\n" + " {\r\n" 256 + " \"name\": \"感冒指数\",\r\n" + " \"level\": \"易发\",\r\n"
257 + " \"msg\": \"感冒容易发⽣,少去⼈密集的场所有利于降低感冒的⼏率。\"\r\n" + " },\r\n" + " {\r\n" 258 + " \"name\": \"洗车指数\",\r\n" + " \"level\": \"不宜\",\r\n"
259 + " \"msg\": \"⾬(雪)⽔和泥⽔会弄脏您的爱车,不适宜清洗车辆。\"\r\n" + " },\r\n" + " {\r\n"
260 + " \"name\": \"穿⾐指数\",\r\n" + " \"level\": \"舒适\",\r\n"
261 + " \"msg\": \"⽩天温度适中,但早晚凉,易穿脱的便携外套很实⽤。\"\r\n" + " },\r\n" + " {\r\n"
262 + " \"name\": \"紫外线强度指数\",\r\n" + " \"level\": \"弱\",\r\n"
263 + " \"msg\": \"辐射较弱,涂擦SPF12-15、PA+护肤品。\"\r\n" + " },\r\n" + " {\r\n"
264 + " \"name\": \"运动指数\",\r\n" + " \"level\": \"不适宜\",\r\n"
265 + " \"msg\": \"受到阵⾬天⽓的影响,不宜在户外运动。\"\r\n" + " }\r\n" + " ],\r\n" + " \"pm25\": {\r\n" 266 + " \"aqi\": 0,\r\n" + " \"co\": 8,\r\n" + " \"o3\": 42,\r\n" + " \"pm10\": 63,\r\n"
267 + " \"pm2_5\": 64,\r\n" + " \"quality\": \"良\",\r\n" + " \"so2\": 4,\r\n"
268 + " \"no2\": 11,\r\n" + " \"updatetime\": \"2017-11-04 13:00:00\"\r\n" + " },\r\n"
269 + " \"daily\": [\r\n" + " {\r\n" + " \"date\": \"2017-11-04\",\r\n"
270 + " \"week\": \"星期六\",\r\n" + " \"sunrise\": \"06:29\",\r\n"
271 + " \"sunset\": \"17:45\",\r\n" + " \"temphigh\": \"25\",\r\n"
272 + " \"templow\": \"19\",\r\n" + " \"weather\": \"多云\"\r\n" + " },\r\n"
273 + " {\r\n" + " \"date\": \"2017-11-05\",\r\n" + " \"week\": \"星期⽇\",\r\n"
274 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
275 + " \"temphigh\": \"26\",\r\n" + " \"templow\": \"19\",\r\n"
276 + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n"
277 + " \"date\": \"2017-11-06\",\r\n" + " \"week\": \"星期⼀\",\r\n"
278 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
279 + " \"temphigh\": \"27\",\r\n" + " \"templow\": \"20\",\r\n"
280 + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n"
281 + " \"date\": \"2017-11-07\",\r\n" + " \"week\": \"星期⼆\",\r\n"
282 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
283 + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"21\",\r\n"
284 + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n"
285 + " \"date\": \"2017-11-08\",\r\n" + " \"week\": \"星期三\",\r\n"
286 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
287 + " \"temphigh\": \"29\",\r\n" + " \"templow\": \"22\",\r\n"
288 + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n"
289 + " \"date\": \"2017-11-09\",\r\n" + " \"week\": \"星期四\",\r\n"
290 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
291 + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"22\",\r\n"
292 + " \"weather\": \"多云\"\r\n" + " },\r\n" + " {\r\n"
293 + " \"date\": \"2017-11-03\",\r\n" + " \"week\": \"星期五\",\r\n"
294 + " \"sunrise\": \"06:29\",\r\n" + " \"sunset\": \"17:45\",\r\n"
295 + " \"temphigh\": \"28\",\r\n" + " \"templow\": \"18\",\r\n"
296 + " \"weather\": \"晴\"\r\n" + " }\r\n" + " ]\r\n" + " },\r\n" + " \"status\": 0,\r\n"
297 + " \"msg\": \"ok\"\r\n" + "}";
298 }
299
300}
需要⽤到的实体类Student
这⾥没有写getter and Setter请⾃⼰添加(后⾯的实体类也是如此)
1import java.util.List;
2
3public class Student {
4 Long id;
5 String name;
6 Integer age;
7 String sex;
8}
需要⽤到的实体类PM25
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论