使用json-lib 数据格式转换
2011-06-25 19:44
537 查看
下载
到http://json-lib.sourceforge.net下载json-lib-1.1-jdk15.jar,运行json还需要的包有:commons-lang.jar、commons-logging.jar,commons-beanutils.jar、xom-1.0-2005-01-05.jar、ezmorph-1.0.1.jar,可在http://www.docjar.com搜索下载。
l 转化数组和集合
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
输出:[true,false,true]
List list = new ArrayList();
list.add(“第一个”);
list.add(“第二个”);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
输出:[“第一个”, “第二个”]
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray3);
输出:[“json”, “is”, “'easy'”]
l 转化对象
转化Map
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 json = JSONObject.fromObject(map);
System.out.println(json);
输出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]
转化Bean
MyBean.java
public class MyBean {
private String name = "json";
private int pojoId = 1;
private String func1 = "function(i){return this.options[i]}";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
//以下为get、set方法
}
MyBean bean = new MyBean();
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
输出:
{"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }
从JSON到Beans
//转化为动态bean
String myjson = "{name=""json"",bool:true,int:1,double:2.2,function:function(a){return a;},array:[1,2]}";
JSONObject json1 = JSONObject.fromString(myjson);
Object bean1 = JSONObject.toBean(json1);
由JSON生成XML
JSONObject json = new JSONObject(true);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(json);
System.out.println("xml:" + xml);
输出:xml:<?xml version="1.0" encoding="UTF-8"?>
<o null="true"/>
JSONObject json2 = JSONObject.fromObject("{""name"":""json"",""bool"":true,""int"":1}");
String xml2 = xmlSerializer.write(json2);
System.out.println("xml2:" + xml2);
输出:xml2:<?xml version="1.0" encoding="UTF-8"?>
<o><bool type="boolean">true</bool><int type="number">1</int><name type="string">json</name></o>
JSONArray json3 = JSONArray.fromObject("[1,2,3]");
String xml3 = xmlSerializer.write(json3);
System.out.println("xml3:" + xml3);
输出:xml3:<?xml version="1.0" encoding="UTF-8"?>
<a><e type="number">1</e><e type="number">2</e><e type="number">3</e></a>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xueyepiaoling/archive/2011/02/24/6204374.aspx
到http://json-lib.sourceforge.net下载json-lib-1.1-jdk15.jar,运行json还需要的包有:commons-lang.jar、commons-logging.jar,commons-beanutils.jar、xom-1.0-2005-01-05.jar、ezmorph-1.0.1.jar,可在http://www.docjar.com搜索下载。
l 转化数组和集合
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);
输出:[true,false,true]
List list = new ArrayList();
list.add(“第一个”);
list.add(“第二个”);
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
输出:[“第一个”, “第二个”]
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray3);
输出:[“json”, “is”, “'easy'”]
l 转化对象
转化Map
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 json = JSONObject.fromObject(map);
System.out.println(json);
输出:[“name”: “json”, “bool”:true, “int”,1, “arr”:[ “a”, “b”], “func”:function(i){return this.arr[i];}]
转化Bean
MyBean.java
public class MyBean {
private String name = "json";
private int pojoId = 1;
private String func1 = "function(i){return this.options[i]}";
private JSONFunction func2 = new JSONFunction(new String[]{"i"},"return this.options[i];");
//以下为get、set方法
}
MyBean bean = new MyBean();
JSONObject jsonObject = JSONObject.fromObject(bean);
System.out.println(jsonObject);
输出:
{"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }
从JSON到Beans
//转化为动态bean
String myjson = "{name=""json"",bool:true,int:1,double:2.2,function:function(a){return a;},array:[1,2]}";
JSONObject json1 = JSONObject.fromString(myjson);
Object bean1 = JSONObject.toBean(json1);
由JSON生成XML
JSONObject json = new JSONObject(true);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(json);
System.out.println("xml:" + xml);
输出:xml:<?xml version="1.0" encoding="UTF-8"?>
<o null="true"/>
JSONObject json2 = JSONObject.fromObject("{""name"":""json"",""bool"":true,""int"":1}");
String xml2 = xmlSerializer.write(json2);
System.out.println("xml2:" + xml2);
输出:xml2:<?xml version="1.0" encoding="UTF-8"?>
<o><bool type="boolean">true</bool><int type="number">1</int><name type="string">json</name></o>
JSONArray json3 = JSONArray.fromObject("[1,2,3]");
String xml3 = xmlSerializer.write(json3);
System.out.println("xml3:" + xml3);
输出:xml3:<?xml version="1.0" encoding="UTF-8"?>
<a><e type="number">1</e><e type="number">2</e><e type="number">3</e></a>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xueyepiaoling/archive/2011/02/24/6204374.aspx
相关文章推荐
- 使用json-lib 数据格式转换
- 【第6篇】使用Json-lib的XMLSerializer的write和reader操作json数据和xml
- 使用json-lib来处理数据(配合jquery)
- Java创建和解析Json数据方法(三)——json-lib包的使用
- 如何使用json-lib解析有2层嵌套的list数据,然后放到对应的bean中
- flex中使用corelib ActionScript 3 Library sdk包解析json数据
- flex中使用corelib ActionScript 3 Library sdk包解析json数据
- java对象和json数据转换实现方式1-使用json-lib实现
- java对象和json数据转换实现方式1-使用json-lib实现
- Java创建和解析Json数据方法(四)——json-lib包的使用
- 使用jackson进行json数据格式转换
- Java创建和解析Json数据方法(三)——json-lib包的使用
- 【第5篇】使用json-lib把json字符串数据转换成Collection
- 客户端使用Ajax访问struts2 , struts2如何返回一个json数据!json-lib与flexjson
- 使用json-lib解析json数据
- [转]flex中使用corelib ActionScript 3 Library sdk包解析json数据
- 使用第三方库json-lib来传递json格式数据
- 使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- 使用FastJson解析Json数据
- 掌握 Ajax,第 10 部分: 使用 JSON 进行数据传输