JsonObject、JsonArray 封装、打印
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class TEST {
public static void main(String[] args) {
--JSONObject是继承Map<String, Object>,并且都是使用的Map中的方法
JSONObject jsonObj = new JSONObject();
jsonObj.put("key1", "value1");
jsonObj.put("key2", "value2");
System.out.println(jsonObj.get("key2")); //value2
--get(String key)返回Object,
--getString(String key)返回String
--getJSONObject(String key)返回JSONObject
--getJSONArray(String key)返回JSONArray
System.out.println(jsonObj.toString()); //{"key1":"value1","key2":"value2"}
--JSONArray是继承List<Object>,并且都是使用的List中的方法,里面可以放JSONObject、ArrayList……
JSONArray jsonArr = new JSONArray();
jsonArr.add(jsonObj);
jsonArr.add(jsonObj);
//[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]
System.out.println(jsonArr.toString());
--拿到index=1的JSONObject,并打印输出{"key1":"value1","key2":"value2"}
System.out.println(jsonArr.get(1));
--get(int index)返回Object,
--getString(int index)返回String
--getInt(int index)返回Int
--getDouble(int index)返回Double
--getJSONObject(int index)返回JSONObject
--getJSONArray(int index)返回JSONArray
System.out.println(jsonArr.getJSONObject(1).get("key1")); //value1
JSONObject jsonObjA = new JSONObject();
jsonObjA.put("itemKey", jsonArr);
//{"itemKey":[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]}
System.out.println(jsonObjA.toString());
--jsonObjA.getJSONArray("itemKey")拿到JSONArray,
--JSONArray再拿到index=1的JSONObject,JSONObject再拿到key="key1"的值value1
System.out.println(jsonObjA.getJSONArray("itemKey").getJSONObject(1).get("key1"));
System.out.println("====================分割线============================");
--String转JSONObject
String jsonObjectStr = "{'key1':'value1','key2':'value2'}";
JSONObject jsonObjectA = JSONObject.fromObject(jsonObjectStr);
System.out.println(jsonObjectA); //{"key1":"value1","key2":"value2"}
--String转JSONArray
String jsonArrayStr = "[{'key1':'value1','key2':'value2'},{'key1':'value3','key2':'value4'}]";
JSONArray jsonArrayA = JSONArray.fromObject(jsonArrayStr);
System.out.println(jsonArrayA); //[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}]
--JSONObject(JSONArray(JSONObject))字符串拆解
String jsonObjArrStr = "{'itemKey':[{'key1':'value1','key2':'value2'},{'key1':'value3','key2':'value4'}]}";
JSONArray jsonArrayB = JSONObject.fromObject(jsonObjArrStr).getJSONArray("itemKey");
--第一种方法
List<Map<String, Object>> resultsA = new ArrayList<>();
Map<String, Object> dataMapA = new HashMap<>();
for(int i=0; i<jsonArrayB.size(); i++){
JSONObject jsonObjectB = jsonArrayB.getJSONObject(i);
dataMapA.put("key1", jsonObjectB.get("key1"));
dataMapA.put("key2", jsonObjectB.get("key2"));
resultsA.add(dataMapA);
}
System.out.println(resultsA); //[{key2=value4, key1=value3}, {key2=value4, key1=value3}]
--第二种方法
@SuppressWarnings({ "unchecked", "static-access" })
--一步提取出来,后面不需要循环,下面的循环只是样式示例
List<HashMap<String, Object>> resultList =
jsonArrayB.toList(jsonArrayB, new HashMap<String, Object>(), new JsonConfig());
System.out.println(resultList); //[{key2=value2, key1=value1}, {key2=value4, key1=value3}]
Map<String, Object> dataMapB = new HashMap<>();
List<Map<String, Object>> resultsB = new ArrayList<>();
for(HashMap<String, Object> map : resultList){
dataMapB.put("key1",map.get("key1"));
dataMapB.put("key2",map.get("key2"));
resultsB.add(dataMapB);
}
System.out.println(resultsB); //[{key2=value4, key1=value3}, {key2=value4, key1=value3}]
}
}
package com.landray.kmss.sys.itpersonal.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
public class TEST {
public static void main(String[] args) {
//JSONObject是继承Map<String, Object>,并且都是使用的Map中的方法
JSONObject jsonObj = new JSONObject();
jsonObj.put("key1", "value1");
jsonObj.put("key2", "value2");
System.out.println(jsonObj.get("key2")); //value2
//get(String key)返回Object,
//getString(String key)返回String
//getJSONObject(String key)返回JSONObject
//getJSONArray(String key)返回JSONArray
System.out.println(jsonObj.toString()); //{"key1":"value1","key2":"value2"}
//JSONArray是继承List<Object>,并且都是使用的List中的方法,里面可以放JSONObject、ArrayList……
JSONArray jsonArr = new JSONArray();
jsonArr.add(jsonObj);
jsonArr.add(jsonObj);
//[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]
System.out.println(jsonArr.toString());
//拿到index=1的JSONObject,并打印输出{"key1":"value1","key2":"value2"}
System.out.println(jsonArr.get(1));
//get(int index)返回Object,
//getString(int index)返回String
//getInt(int index)返回Int
//getDouble(int index)返回Double
//getJSONObject(int index)返回JSONObject
//getJSONArray(int index)返回JSONArray
System.out.println(jsonArr.getJSONObject(1).get("key1")); //value1
JSONObject jsonObjA = new JSONObject();
jsonObjA.put("itemKey", jsonArr);
//{"itemKey":[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]}
System.out.println(jsonObjA.toString());
//jsonObjA.getJSONArray("itemKey")拿到JSONArray,
//JSONArray再拿到index=1的JSONObject,JSONObject再拿到key="key1"的值value1
System.out.println(jsonObjA.getJSONArray("itemKey").getJSONObject(1).get("key1"));
System.out.println("====================分割线============================");
//String转JSONObject
String jsonObjectStr = "{'key1':'value1','key2':'value2'}";
JSONObject jsonObjectA = JSONObject.fromObject(jsonObjectStr);
System.out.println(jsonObjectA); //{"key1":"value1","key2":"value2"}
//String转JSONArray
String jsonArrayStr = "[{'key1':'value1','key2':'value2'},{'key1':'value3','key2':'value4'}]";
JSONArray jsonArrayA = JSONArray.fromObject(jsonArrayStr);
System.out.println(jsonArrayA); //[{"key1":"value1","key2":"value2"},{"key1":"value3","key2":"value4"}]
//JSONObject(JSONArray(JSONObject))字符串拆解
String jsonObjArrStr = "{'itemKey':[{'key1':'value1','key2':'value2'},{'key1':'value3','key2':'value4'}]}";
JSONArray jsonArrayB = JSONObject.fromObject(jsonObjArrStr).getJSONArray("itemKey");
//第一种方法
List<Map<String, Object>> resultsA = new ArrayList<>();
Map<String, Object> dataMapA = new HashMap<>();
for(int i=0; i<jsonArrayB.size(); i++){
JSONObject jsonObjectB = jsonArrayB.getJSONObject(i);
dataMapA.put("key1", jsonObjectB.get("key1"));
dataMapA.put("key2", jsonObjectB.get("key2"));
resultsA.add(dataMapA);
}
System.out.println(resultsA); //[{key2=value4, key1=value3}, {key2=value4, key1=value3}]
//第二种方法
@SuppressWarnings({ "unchecked", "static-access" })
//一步提取出来,后面不需要循环,下面的循环只是样式示例
List<HashMap<String, Object>> resultList =
jsonArrayB.toList(jsonArrayB, new HashMap<String, Object>(), new JsonConfig());
System.out.println(resultList); //[{key2=value2, key1=value1}, {key2=value4, key1=value3}]
Map<String, Object> dataMapB = new HashMap<>();
List<Map<String, Object>> resultsB = new ArrayList<>();
for(HashMap<String, Object> map : resultList){
dataMapB.put("key1",map.get("key1"));
dataMapB.put("key2",map.get("key2"));
resultsB.add(dataMapB);
}
System.out.println(resultsB); //[{key2=value4, key1=value3}, {key2=value4, key1=value3}]
}
}
文章链接:JSONObject、JSONArray 非空判断