net.sf.json.JSOnArrayとnet.sf.json.JSOnObjectメソッドまとめ

16399 ワード

この間は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       0  >   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
" + " \"data\": {\r
" + " \"city\": \" \",\r
" + " \"temphigh\": \"25\",\r
" + " \"templow\": \"19\",\r
" + " \"updatetime\": \"2017-11-04 13:23:00\",\r
" + " \"tempnow\": \"24\",\r
" + " \"sendibletemp\": \"27\",\r
" + " \"winddirect\": \" \",\r
" + " \"windpower\": \"2 \",\r
" + " \"humidity\": \"42\",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"weather\": \" \",\r
" + " \"week\": \" \",\r
" + " \"nl\": null,\r
" + " \"date\": \"2017-11-04\",\r
" + " \"index\": [\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" , , , 。\"\r
" + " },\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" , 。\"\r
" + " },\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" ( ) , 。\"\r
" + " },\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" , , 。\"\r
" + " },\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" , SPF12-15、PA+ 。\"\r
" + " },\r
" + " {\r
" + " \"name\": \" \",\r
" + " \"level\": \" \",\r
" + " \"msg\": \" , 。\"\r
" + " }\r
" + " ],\r
" + " \"pm25\": {\r
" + " \"aqi\": 0,\r
" + " \"co\": 8,\r
" + " \"o3\": 42,\r
" + " \"pm10\": 63,\r
" + " \"pm2_5\": 64,\r
" + " \"quality\": \" \",\r
" + " \"so2\": 4,\r
" + " \"no2\": 11,\r
" + " \"updatetime\": \"2017-11-04 13:00:00\"\r
" + " },\r
" + " \"daily\": [\r
" + " {\r
" + " \"date\": \"2017-11-04\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"25\",\r
" + " \"templow\": \"19\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-05\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"26\",\r
" + " \"templow\": \"19\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-06\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"27\",\r
" + " \"templow\": \"20\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-07\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"28\",\r
" + " \"templow\": \"21\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-08\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"29\",\r
" + " \"templow\": \"22\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-09\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"28\",\r
" + " \"templow\": \"22\",\r
" + " \"weather\": \" \"\r
" + " },\r
" + " {\r
" + " \"date\": \"2017-11-03\",\r
" + " \"week\": \" \",\r
" + " \"sunrise\": \"06:29\",\r
" + " \"sunset\": \"17:45\",\r
" + " \"temphigh\": \"28\",\r
" + " \"templow\": \"18\",\r
" + " \"weather\": \" \"\r
" + " }\r
" + " ]\r
" + " },\r
" + " \"status\": 0,\r
" + " \"msg\": \"ok\"\r
" + "}"; } }

使用するエンティティークラスStudent
ここにgetter and Setterは書いていませんので自分で追加してください(後ろのエンティティクラスもそうです)
import java.util.List;

public class Student {
	Long id;
	String name;
	Integer age;
	String sex;
}

使用するエンティティークラスPM 25
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;
}