开始使用JSON lib——前后台交互
2014-05-14 10:56
302 查看
1. json-lib是一个java类库,提供将Java对象,包括beans, maps, collections, java arrays and XML等转换成JSON,或者反向转换的功能。
2. json-lib 主页 : http://json-lib.sourceforge.net/
find-jar(用来查找jar包):http://www.findjar.com/index.x
3.执行环境
需要以下类库支持
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
4.测试
package com.bipaas.utility;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TransJSON {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//List集合转换成json代码
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray json1 = JSONArray.fromObject(list);
System.out.println(json1+"………………json1");
//Map集合转换成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 json2 = JSONObject.fromObject(map);
System.out.println(json2+"………………json2");
//Bean转换成json代码
// JSONObject jsonObject = JSONObject.fromObject(new JSONBean());
//数组转换成json代码
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
System.out.println(jsonArray1+"………………jsonArray1");
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject json3 = JSONObject.fromObject( json );
System.out.println(json3+"………………json3");
String json4 = "{bool:true,integer:1,string:\"json\"}";
JSONObject jsonObject3 = JSONObject.fromObject( json4 );
System.out.println(json4+"………………json4");
//一般数据转换成json代码
JSONArray jsonArray2 = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray2+"………………jsonArray2");
}
}
5.执行结果
log4j:ERROR No appender named [console] could be found.
["first","second"]………………json1
{"arr":["a","b"],"int":1,"name":"json","func":function(i){return this.arr[i];},"bool":true}………………json2
[true,false,true]………………jsonArray1
{"name":"json","bool":true,"int":1,"double":2.2,"func":function(a){ return a; },"array":[1,2]}………………json3
{bool:true,integer:1,string:"json"}………………json4
["json","is","easy"]………………jsonArray2
2. json-lib 主页 : http://json-lib.sourceforge.net/
find-jar(用来查找jar包):http://www.findjar.com/index.x
3.执行环境
需要以下类库支持
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
4.测试
package com.bipaas.utility;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class TransJSON {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//List集合转换成json代码
List list = new ArrayList();
list.add("first");
list.add("second");
JSONArray json1 = JSONArray.fromObject(list);
System.out.println(json1+"………………json1");
//Map集合转换成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 json2 = JSONObject.fromObject(map);
System.out.println(json2+"………………json2");
//Bean转换成json代码
// JSONObject jsonObject = JSONObject.fromObject(new JSONBean());
//数组转换成json代码
boolean[] boolArray = new boolean[]{true,false,true};
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
System.out.println(jsonArray1+"………………jsonArray1");
String json = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";
JSONObject json3 = JSONObject.fromObject( json );
System.out.println(json3+"………………json3");
String json4 = "{bool:true,integer:1,string:\"json\"}";
JSONObject jsonObject3 = JSONObject.fromObject( json4 );
System.out.println(json4+"………………json4");
//一般数据转换成json代码
JSONArray jsonArray2 = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray2+"………………jsonArray2");
}
}
5.执行结果
log4j:ERROR No appender named [console] could be found.
["first","second"]………………json1
{"arr":["a","b"],"int":1,"name":"json","func":function(i){return this.arr[i];},"bool":true}………………json2
[true,false,true]………………jsonArray1
{"name":"json","bool":true,"int":1,"double":2.2,"func":function(a){ return a; },"array":[1,2]}………………json3
{bool:true,integer:1,string:"json"}………………json4
["json","is","easy"]………………jsonArray2
相关文章推荐
- .NetMVC与EXT4使用json进行前后台交互
- 前后台交互使用json所要用到的jar包下载
- Extjs和Asp.NET后台的数据交互(一) => Newtonsoft.Json 使用简介
- Spring3 MVC 使用JSON进行前后台数据交互
- 在ssm框架中前后台数据交互均使用json格式
- EXTJS与java后台交互(二)JSON使用
- jquery与后台交互,传递json对象的使用方法
- Play前后台使用JsonArray交互,以及使用JQuery遍历array数组和JsonArray
- Ajax使用json前后台交互
- .Net3.5中Json序列化及其在前后台交互中的使用
- json-lib.jar使用方法详解(动态从后台获取Javascript函数)
- EXTJS与java后台交互(二)JSON使用
- EXTJS与java后台交互(二)JSON使用
- 关于使用jquery与后台交互,传递json对象的使用方法
- 如何使用 How to use json-lib
- [转]flex中使用corelib ActionScript 3 Library sdk包解析json数据
- 使用JSON-LIB转换JAVA对象
- json-lib.jar开发包使用
- json-lib.jar开发包使用
- 使用json-lib的JSONObject.toBean( )时碰到的日期属性转换的问题