网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

json-lib简介

http://www.diybl.com/ 2008-1-28  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

JSON-lib这个Java类包用于把bean,map和XML转换成JSON并能够把JSON转回成bean和DynaBean。
下载地址:http://json-lib.sourceforge.net

还要需要的第3方包:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom

How to use json-lib

Using the JSONSerializer

Working with arrays and collections

Working with objects

Working with XML


Using the JSONSerializer

JSONSerializer can transform any java object to JSON notation and back with a simple and clean interface, leveraging all the builders in JSONObject and JSONArray. To transform a java obect into JSON use JSONSerializer.toJSON(). To transform a valid JSON value (by JSON, I mean an Object implementing that interface), use toJava(). The last method is an instance method because the serializer needs special configuration to transform a JSON value to a bean class, array, List or DynaBean.


Working with arrays and collections

The simplest way to create a JSONArray from a java array or collection is through the static factory methods from JSONArray. JSONArray.fromObject() will inspect its parameter and call the correct factory or constructor.

Examples:



  1. boolean[] boolArray = new boolean[]{true,false,true};   
  2. JSONArray jsonArray = JSONArray.fromObject( boolArray );   
  3. System.out.println( jsonArray );   
  4. // prints [true,false,true]  


  1. List list = new ArrayList();   
  2. list.add( "first" );   
  3. list.add( "second" );   
  4. JSONArray jsonArray = JSONArray.fromObject( list );   
  5. System.out.println( jsonArray );   
  6. // prints ["first","second"]  

文章整理:DIY部落 http://www.diybl.com (andyyoo)   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言