1. 数组与List、Collection等都用JSONArray解析
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject( boolArray );
System.out.println( jsonArray );
// prints [true,false,true]
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray = JSONArray.fromObject( list );
System.out.println( jsonArray );
// prints ["first","second"]
JSONArray jsonArray = JSONArray.fromObject( "['json','is','easy']" );
System.out.println( jsonArray );
// prints ["json","is","easy"]
2. Beans & Maps 转换 to JSON
Map map = new HashMap();
map.put( "name", "json" );
map.put( "bool", Boolean.TRUE );
map.put( "int", new Integer(1) );
map.put( "arr", new String[]{"a","b"} );
map.put( "func", "function(i){ return this.arr[i]; }" ); JSONObject jsonObject = JSONObject.fromObject( map );
System.out.println( jsonObject );
// prints ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ return this.arr[i]; }] class MyBean{
private String name = "json";
private int pojoId = 1;
private char[] options = new char[]{'a','f'};
private String func1 = "function(i){ return this.options[i]; }";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];"); // getters & setters
...
} JSONObject jsonObject = JSONObject.fromObject( new MyBean() );
System.out.println( jsonObject );
/* prints
{"name":"json","pojoId":1,"options":["a","f"],
"func1":function(i){ return this.options[i];},
"func2":function(i){ return this.options[i];}}
*/

3.From JSON to Beans

Convert to DynaBean:

String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject jsonObject = JSONObject.fromObject( json );
Object bean = JSONObject.toBean( jsonObject );
assertEquals( jsonObject.get( "name" ), PropertyUtils.getProperty( bean, "name" ) );
assertEquals( jsonObject.get( "bool" ), PropertyUtils.getProperty( bean, "bool" ) );
assertEquals( jsonObject.get( "int" ), PropertyUtils.getProperty( bean, "int" ) );
assertEquals( jsonObject.get( "double" ), PropertyUtils.getProperty( bean, "double" ) );
assertEquals( jsonObject.get( "func" ), PropertyUtils.getProperty( bean, "func" ) );
List expected = JSONArray.toList( jsonObject.getJSONArray( "array" ) );
Assertions.assertListEquals( expected, (List) PropertyUtils.getProperty( bean, "array" ) );

Convert to Bean:

String json = "{bool:true,integer:1,string:\"json\"}";
JSONObject jsonObject = JSONObject.fromObject( json );
BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class );
assertEquals( jsonObject.get( "bool" ), Boolean.valueOf( bean.isBool() ) );
assertEquals( jsonObject.get( "integer" ), new Integer( bean.getInteger() ) );
assertEquals( jsonObject.get( "string" ), bean.getString() );
Working with XML
From JSON to XML
JSONObject json = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");
String xml = XMLSerializer.write( json );
From XML to JSON
JSONArray json = (JSONArray) XMLSerializer.read( xml );
System.out.println( json );
// prints [function(i,j){ return matrix[i][j]; }]

如何使用Json-lib的更多相关文章

  1. Json lib集成stucts2的使用方法 抛出 NestableRuntimeException异常的解决办法

    首先贴出struts 2.3.16需要导入的包 因为使用的是2.3 版本,必须要导入这个包,否则会报java.lang.NoClassDefFoundError: org/apache/commons ...

  2. 使用JsonConfig控制JSON lib序列化

    将对象转换成字符串,是非常常用的功能,尤其在WEB应用中,使用 JSON lib 能够便捷地完成这项工作.JSON lib能够将Java对象转成json格式的字符串,也可以将Java对象转换成xml格 ...

  3. Atitit.json类库的设计与实现 ati json lib

    Atitit.json类库的设计与实现 ati json lib 1. 目前jsonlib库可能有问题,可能版本冲突,抛出ex1 2. 解决之道:1 2.1. 自定义json解析库,使用多个复合的js ...

  4. JSON lib 里JsonConfig详解

    一,setCycleDetectionStrategy 防止自包含 /** * 这里测试如果含有自包含的时候需要CycleDetectionStrategy */ public static void ...

  5. json lib 2.4及其依赖包下载

    下载文件地址:https://files.cnblogs.com/files/xiandedanteng/json-lib-2.4%26dependencies_jars.rar 它包括 common ...

  6. java中Array/List/Map/Object与Json互相转换详解

    http://blog.csdn.net/xiaomu709421487/article/details/51456705 JSON(JavaScript Object Notation): 是一种轻 ...

  7. java 读写JSON(一)

    算是第一次正式接触Json,没有深入研究,先贴上java的代码,日后才说! package priv.chenhy.datehandle; import java.io.BufferedReader; ...

  8. spingmvc 返回json数据日期格式化方法

    第一种: json 用的是这个依赖 <!-- JSON lib 开发包 以及它的依赖包 --> <dependency> <groupId>com.fasterxm ...

  9. Struts2+JSON+JQUERY DEMO

    看到别人用了Struts2和JSON,自己也想练练手.记录下练习过程中遇到的问题,以便参考. 使用Maven新建项目: 先挂上pom.xml <project xmlns="http: ...

  10. net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案

    我使用的net.sf.json是json-lib-2.4-jdk15.jar,把json对象转换为普通java实体对象时候有个问题,josn对象转换为java对象之后,json串里面的那几个小数点的值 ...

随机推荐

  1. android onTouch()与onTouchEvent()的区别

    1.onTouch方法: onTouch方法是View的 OnTouchListener借口中定义的方法.当一个View绑定了OnTouchLister后,当有touch事件触发时,就会调用onTou ...

  2. key_t键和ftok函数

    系统建立IPC通讯(如消息队列.共享内存时)必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型如下: key_t ftok( char * fname, int id ) f ...

  3. UML类图中类与类的四种关系图解

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  4. SVN高速新手教程

    因为做Zip和Rar解析的时候,找到了mucommander工具,可将其jar包导入项目里执行发现报:类型转换错误,org.apache.commons.logging.impl.Log4JLogge ...

  5. android93 进程优先级补充

    ###进程优先级(一个应用可以有多个进程,比如有Activity的进程和service的进程,)* 五种前台进程 * 拥有一个正在与用户交互的activity(onResume调用)的进程 * 拥有一 ...

  6. Tsar 服务器系统和应用信息的采集报告工具

    Tsar介绍 Tsar是淘宝的一个用来收集服务器系统和应用信息的采集报告工具,如收集服务器的系统信息(cpu,mem等),以及应用数据(nginx.swift等),收集到的数据存储在服务器磁盘上,可以 ...

  7. 更改Mysql数据库存储位置的具体步骤

    首先把mysql的服务先停掉,更改MySQL配置文件My.ini中的数据库存储主路径,将老的数据库存储主路径中的数据库文件和文件夹复制到新的存储主路径,接下来重启搞定     一.首先把mysql的服 ...

  8. 有关Color和Drawable你所不知道的那些内容

    Android开发中,我们经常会用到Color或Drawable,有时他们是可以混用的,有时却有严格的区别. Drawable 体系结构 Drawable是可绘制物件的一般抽象.与View不同,Dra ...

  9. Const和readonly这间的区别和相同处

    相同:  const和readonly都是用来修饰常量的 不同: const 在申明之前就要对它初始化,readonly修饰的常量则可以到构造函数中初始化 const注重的是效率但是readonly注 ...

  10. ZBLibrary不能运行的解决方法

    ADT运行   右键ZBLibrary Demo > Run as > Android Application 可能出现部分错误,解决方案如下: 右键Project(这里为ZBLibrar ...