Json-lib 進行java與json字串轉換之二

來源:互聯網
上載者:User

標籤: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字串轉換之二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.