json(JavaScript Object Natation)學習

來源:互聯網
上載者:User

標籤:

Json必需的包:

commons-httpclient-3.1.jarcommons-lang-2.4.jarcommons-logging-1.1.1.jarjson-lib-2.2.3-jdk13.jarezmorph-1.0.6.jarcommons-collections-3.2.1.jar

注意:

java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher錯誤
是因為沒有匯入ezmorph.jar檔案或版本不對。

java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap錯誤
是因為沒有匯入commons-collections.jar檔案或版本不對。

Java代碼轉換成json代碼:

1. List集合轉換成json代碼

List list = new ArrayList(); list.add( "first" ); list.add( "second" ); JSONArray jsonArray2 = JSONArray.fromObject( list );

2. Map集合轉換成json代碼

Map map = new HashMap(); map.put("name", "json"); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr; }"); JSONObject json = JSONObject.fromObject(map);

3. Bean轉換成json代碼

JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

4. 數群組轉換成json代碼

boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

5. 一般資料轉換成json代碼

JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );

6. beans轉換成json代碼

List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list);

7.JSON轉換成相應的bean

ClassA  a  = (ClassA)JSONObject.toBean(jsonObject,ClassA.class);

 

json(JavaScript Object Natation)學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.