到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>
分享到:
相关推荐
使用json-lib实现json与Java对象的互转
使用json-lib解析json所需要的jar,包括: commons-beanutils-1.7.0.jar、commons-collections-3.2.jar、 commons-lang-2.4.jar、commons-logging-1.1.jar、 ezmorph-1.0.4.jar、json-lib-2.2.2-jdk15.jar
json-lib-2.4-jdk15.jar下载,json-lib是一款简单易用的Java JSON库。它可以将Java对象转换成JSON字符串,也可以将JSON字符串转换成Java对象。json-lib体积小、无依赖,性能表现也不错,适合在各种Java项目中使用。 ...
json-lib-2.2.3-jdk15.jar commons-beanutils-1.7.0.jar commons-httpclient-3.1.jar commons-lang-2.3.jar commons-logging-1.1.1.jar commons-collections-3.2.1.jar ezmorph-1.0.3.jar 如果不关联这些包,则会...
1、json的核心包:json-lib-2.4-jdk15.jar; 2、json-lib-2.4-jdk15.jar依赖的jar包:commons-beanutils-1.9.3.jar,commons-collections-3.2.2.jar,commons-lang-2.6.jar,commons-logging-1.2.jar,ezmorph-1.0.6...
json架包,包好json-lib-2.2.3-jdk13.jar,json-lib-2.4-jdk14.jar以及附带的支持包ezmorph-1.0.6.jar,commons-logging.jar,commons-lang.jar等
JSON-lib包json-lib-0.8.jar和json-lib-0.9.jar包
里面包含json-lib所需的全部jar文件. json-lib-2.1-jdk15.jar ezmorph-1.0.3.jar commons-beanutils.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar
<groupId>net.sf.json-lib</groupId> <artifactId>json-lib <version>2.4 <classifier>jdk15 或者直接下载这个包
使用json-lib重写toJsonString方法,从而方便处理后台数据显示到前台的情况。
json-lib-2.4-jdk15所需全部JAR包,资源清单 json-lib-2.4-jdk15.jar 依赖: ezmorph-1.0.6.jar commons-beanutils-1.8.3.jar commons-lang-2.5.jar morph-1.1.1.jar commons-logging-1.1.1.jar commons-collections-...
json-lib-2.4-jdk15依赖的库文件: 压缩包内容: commons-beanutils-1.8.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15-javadoc.jar ...
json-lib-2.4-jdk15-sources.jar 是net.sf.json-lib-jdk15-2.4的源码文件
json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar 包含文件有:json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar
json基础jar包,包含json-lib-2.3-jdk15.jar源码
json的核心包:json-lib-2.4-jdk15.jar;json-2.4所依赖的jar包:commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar;以及和...
json-lib-2.3-jdk15开发包和所有依赖包
json-lib-1.1-jdk13-javadoc,欢迎下载。
json-lib2.2.3-sdk15及json-lib依赖的所有...commons-beanutils-1.8.0.jar\commons-collections-3.2.jar\commons-lang-2.4.jar\commons-logging-1.1.1.jar\ezmorph-1.0.6.jar\json-lib-2.2.3-jdk15.jar\xom-1.2.6.jar