java的json解析工具_json在java中的几种解析工具的使用
json一般有兩種結構,json的官方介紹可以參考?http://www.json.org/,這里不再贅述。java中json的解析工具有很多,如json-lib,org-json,fast-json,jackson等,這里選擇幾種解析工具簡要說明一下它們的使用方式。
json-lib的使用
JsonObject.from(Object object)
JsonArray.from(Object object)
這兩種方式可以創建json對象,前者可以傳入鍵值對形式的字符串,map,javaBean;后者可以傳入數組對象,javascript格式的數組字符串,set集合,list集合。
基本使用:
String str = "{'list':\"[{'name':'gs', 'age':20}]\"}";
JSONObject jo = JSONObject.fromObject(str);
System.out.println(jo.toString());
Object object = jo.get("list");
JSONArray ja = JSONArray.fromObject(object);
System.out.println(ja);
//{"list":"[{'name':'gs', 'age':20}]"}//[{"name":"gs","age":20}]
/*** 對象數組轉json數組對象*/
@Test
public void objArr2Ja(){
Object[] o = { "北京", "上海", 89, true, 90.87 };
JSONArray json = JSONArray.fromObject(o);
System.out.println(json);//["北京","上海",89,true,90.87]
}
另外,還可以傳入一個 JsonConfig參數,對于json解析做一些限制。
JsonConfig config = new JsonConfig();
config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//防止自循環,當person對象類擁有自身類型的屬性時,會循環生成json字符串,這是不允許的。
JSONObject jo = JSONObject.fromObject(person, config);
String s = "{'name':'20','age':'30'}";
JsonConfig config = new JsonConfig();
config.setExcludes(new String[]{"name"}); //排除不需要轉化為json的屬性,主要針對JSONObject對象
JSONObject json = JSONObject.fromObject(s,config);
System.out.println(json);
Map map = new HashMap();
map.put("name", "json");
map.put("class", "ddd");
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(true); //輸出json時是否忽略 要排除的默認屬性,默認值false表示不忽略,此處true表示忽略。
JSONObject jsonObject = JSONObject.fromObject(map,config);
private static final String[] DEFAULT_EXCLUDES = new String[] { "class", "declaringClass", "metaClass" }; //源碼中默認會過濾的幾個key
registerJsonBeanProcessor 當value類型是從java的一個bean轉化過來的時候,可以提供自定義處理器
public static void testMap() {
Map map = new HashMap();
map.put("name", "json");
map.put("class", "ddd");
map.put("date", new Date());
JsonConfig config = new JsonConfig();
config.setIgnoreDefaultExcludes(false);
config.registerJsonBeanProcessor(Date.class,
new JsDateJsonBeanProcessor()); //當輸出時間格式時,采用和JS兼容的格式輸出
JSONObject jsonObject = JSONObject.fromObject(map, config);
System.out.println(jsonObject);
}
結果:{"date":{"year":2017,"month":0,"day":20,"hours":23,"minutes":15,"seconds":54,"milliseconds":615},"name":"json"}
對于JsonObject對象,獲取鍵值時,建議使用JSONObject.get()方法,不要使用JSONobject.getString()等指定返回結果類型的方法;因為當鍵不存在或鍵存在但轉換類型異常時,后者會直接拋異常,而鍵不存在時前者會直接返回一個null對象。
org-json的使用
構建json字符串
使用JSONObject的無參構造方法,然后調用put()方法,最后toString()。無序的
1
2String string =new JSONObject().put("name","gs").put("school","jinan").toString();
System.out.println(string);
使用JSONObject的有參構造方法,傳入參數字符串。無序的
1
2
3String str ="{'x':'5',? j:88t,? k: 72,? y:tfj, \"z\":m}";
JSONObject jo =new JSONObject(str);
System.out.println(jo.toString());//{"x":"5","y":"tfj","j":"88t","z":"m","k":72}
jo = new JSONObject("{k:v,k2:v2,k3:[1,{k4:V4}]}");
System.out.println(jo.toString());//{"k2":"v2","k3":[1,{"k4":"V4"}],"k":"v"}
1
2JSONArray ja =new JSONArray("[{x:5,y:7,z:10}]");
System.out.println(ja.toString());//[{"x":5,"y":7,"z":10}]
String s = "{hpys=2, hpgs=0, jgsk=170118042446, cllx=H1, hphm=AV1738, carPic=http://172.16.56.204:8088/g1/M00/00/00/rBA4zFh-gS-IXyLNAACW0w5Ti0wAAAAQwNjyY8AAJbr858.jpg}";
JSONObject jo = new JSONObject(s);
System.out.println(jo); //錯誤的,值中不能出現特殊字符/,:
java.lang.ClassCastException: org.json.JSONObject cannot be cast to java.util.Map
String str = "{x=5,y=7,z=10}";
JSONObject jo = new JSONObject(str);
Map map = (Map)jo;
使用JSONStringer()類
String string2 = new JSONStringer().object().key("name").value("gs").key("school").value("jinna").endObject().toString();
其實,json-lib中也有類似的兩個方法。
而且,使用中發現通過JSONObject構造的json文本順序混亂,但是JSONStringer卻是有序的。
2. 獲取Json對象的鍵值對
方法與json-lib相似,都有get(),getInt()等方法,但是如果鍵不存在,使用get()方法或getXXX()方法都會拋出異常,這是與json-lib截然不同的,使用者需要格外注意。其他方面大致相同。
總結
以上是生活随笔為你收集整理的java的json解析工具_json在java中的几种解析工具的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: I.MX6ULL芯片介绍 —— 迅为
- 下一篇: 当winform窗体的Bordestyl