標籤:name each .class listt json-lib 協助 方式 static ddr
二、list和json字串的互轉
list--》》json字串
public static void listToJSON(){Student stu=new Student();stu.setName("JSON");stu.setAge("23");stu.setAddress("北京市海澱區");List<Student> lists=new ArrayList<Student>();lists.add(stu);//1、使用JSONObject//JSONObject listObject=JSONObject.fromObject(lists);//2、使用JSONArrayJSONArray listArray=JSONArray.fromObject(lists);//System.out.println("listObject:"+listObject.toString());System.out.println("listArray:"+listArray.toString());}
我把使用JSONObject的方式給注掉了,我們先看注釋之前的結果,
Exception in thread "main" net.sf.json.JSONException: ‘object‘ is an array. Use JSONArray instead
告訴我說有一個異常,通過查看源碼發現,在使用fromObject方法的時候會先進行參數類型的判斷,這裡就告訴我們,傳入的參數是一個array類型,因為使用的ArrayList,再來看,注釋之後的結果,
listArray:[{"address":"北京市海澱區","age":"23","name":"JSON"}]
這樣結果是正常的。
json字串--》》list
從上面的例子可以看出list的對象只能轉化為數組對象的格式,那麼我們看下面的字串到list的轉化,
public static void jsonToList(){String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城區\"}]";//轉化為listList<Student> list2=(List<Student>)JSONArray.toList(JSONArray.fromObject(arrayStr), Student.class);for (Student stu : list2) {System.out.println(stu);}//轉化為數組Student[] ss =(Student[])JSONArray.toArray(JSONArray.fromObject(arrayStr),Student.class);for (Student student : ss) {System.out.println(student);}}
列印結果,
Student [name=JSON, age=24, address=北京市西城區]
Student [name=JSON, age=24, address=北京市西城區]
由於字串的格式為帶有“[]”的格式,所以這裡選擇JSONArray這個對象,它有toArray、toList方法可供使用,前者轉化為 java中的數組,或者轉化為java中的list,由於這裡有實體類進行對應,所以在使用時指定了泛型的類型(Student.class),這樣就可 以得到轉化後的對象。
三、map和json字串的互轉
map--》》json字串
public static void mapToJSON(){Student stu=new Student();stu.setName("JSON");stu.setAge("23");stu.setAddress("中國上海");Map<String,Student> map=new HashMap<String,Student>();map.put("first", stu);//1、JSONObjectJSONObject mapObject=JSONObject.fromObject(map);System.out.println("mapObject"+mapObject.toString());//2、JSONArrayJSONArray mapArray=JSONArray.fromObject(map);System.out.println("mapArray:"+mapArray.toString());}
列印結果,
mapObject{"first":{"address":"中國上海","age":"23","name":"JSON"}}
mapArray:[{"first":{"address":"中國上海","age":"23","name":"JSON"}}]
上面列印了兩種形式。
json字串--》》map
JSON字串不能直接轉化為map對象,要想取得map中的鍵對應的值需要別的方式,
public static void jsonToMap(){String strObject="{\"first\":{\"address\":\"中國上海\",\"age\":\"23\",\"name\":\"JSON\"}}";//JSONObjectJSONObject jsonObject=JSONObject.fromObject(strObject);Map map=new HashMap();map.put("first", Student.class);//使用了toBean方法,需要三個參數 MyBean my=(MyBean)JSONObject.toBean(jsonObject, MyBean.class, map);System.out.println(my.getFirst());}
列印結果,
Student [name=JSON, age=23, address=中國上海]
下面是MyBean的代碼,
package com.cn.study.day4;import java.util.Map;import com.cn.study.day3.Student;public class MyBean {private Student first;public Student getFirst() {return first;}public void setFirst(Student first) {this.first = first;}}
使用toBean()方法是傳入了三個參數,第一個是JSONObject對象,第二個是MyBean.class,第三個是一個Map對象。通過 MyBean可以知道此類中要有一個first的屬性,且其類型為Student,要和map中的鍵和實值型別對應,即,first對應鍵 first類型對應值的類型。
以上所述是小編給大家介紹的JSON字串與java對象的相互轉換執行個體詳解,希望對大家有所協助!
原文連結:http://www.cnblogs.com/teach/archive/2016/08/20/5791029.html
Json-lib 進行java與json字串轉換之二