JSON-lib 使用
来源:互联网 发布:淘宝包邮是怎么做到的 编辑:程序博客网 时间:2024/06/08 02:39
http://weishuwei.iteye.com/blog/97384
json-lib.jar开发包使用:
依赖包:
commons-beanutils.jar;
commons-httpclient.jar;
commons-lang.jar;
ezmorph.jar;不少人使用时会提示net.sf.ezmorph.xxx找不到,就是缺这个:
morph-1.0.1.jar
相关链接:
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
使用过程中问题:
1,把bean转化为json格式时老提示如下错误:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
解决:声明bean为public class xxx,必须是public,我用默认类型(class xxx)都不行
2,Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.toObject([C)[Ljava/lang/Character;
原因:定义属性如下:private char[] options = new char[] { 'a', 'f' };好像不能处理这种类型的
3, private String func1 = "function(i){ return this.options[i]; }";
和
private JSONFunction func2 = new JSONFunction(new String[] { "i" },
"return this.options[i];");
转换后显示结果差不多:
{"func1":function(i){ return this.options[i];,"func2":function(i){ return this.options[i]; }}
依赖包:
commons-beanutils.jar;
commons-httpclient.jar;
commons-lang.jar;
ezmorph.jar;不少人使用时会提示net.sf.ezmorph.xxx找不到,就是缺这个:
morph-1.0.1.jar
相关链接:
http://json-lib.sourceforge.net/
http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
使用过程中问题:
1,把bean转化为json格式时老提示如下错误:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
解决:声明bean为public class xxx,必须是public,我用默认类型(class xxx)都不行
2,Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.toObject([C)[Ljava/lang/Character;
原因:定义属性如下:private char[] options = new char[] { 'a', 'f' };好像不能处理这种类型的
3, private String func1 = "function(i){ return this.options[i]; }";
和
private JSONFunction func2 = new JSONFunction(new String[] { "i" },
"return this.options[i];");
转换后显示结果差不多:
{"func1":function(i){ return this.options[i];,"func2":function(i){ return this.options[i]; }}
测试类:
- 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 Json {
- public static void main(String[] args) {
- Json j = new Json();
- j.bean2json();
- }
- public void arr2json() {
- boolean[] boolArray = new boolean[] { true, false, true };
- JSONArray jsonArray = JSONArray.fromObject(boolArray);
- System.out.println(jsonArray);
- // prints [true,false,true]
- }
- public void list2json() {
- List list = new ArrayList();
- list.add("first");
- list.add("second");
- JSONArray jsonArray = JSONArray.fromObject(list);
- System.out.println(jsonArray);
- // prints ["first","second"]
- }
- public void createJson() {
- JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
- System.out.println(jsonArray);
- // prints ["json","is","easy"]
- }
- public void map2json() {
- Map
- 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);
- // prints
- // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
- // return this.arr[i]; }]
- }
- public void bean2json() {
- JSONObject jsonObject = JSONObject.fromObject(new MyBean());
- System.out.println(jsonObject);
- /*
- * prints
- * {"func1":function(i){ return this.options[i];
- * },"pojoId":1,"name":"json","func2":function(i){ return
- * this.options[i]; }}
- */
- }
- public void json2bean() {
- String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
- JSONObject jb = JSONObject.fromString(json);
- JSONObject.toBean(jb, MyBean.class);
- System.out.println();
- }
- }
操作的bean:
- import net.sf.json.JSONFunction;
- public 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
- ......
- }
题外话: 这个我对json-lib包的初次尝试,希望对大家有所帮助,另外大家有谁用过其它处理json的开发包,提出来,大家探讨一下~!!!!
=============
java中使用net.sf.json对json进行解析
2010-08-02 15:44 1869人阅读 评论(0)收藏举报
net.sf.json依赖的包很多。
有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar
- /**
- * 从一个JSON 对象字符格式中得到一个java对象,形如:
- * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
- * @param object
- * @param clazz
- * @return
- */
- public static Object getDTO(String jsonString, Class clazz){
- JSONObject jsonObject = null;
- try{
- setDataFormat2JAVA();
- jsonObject = JSONObject.fromObject(jsonString);
- }catch(Exception e){
- e.printStackTrace();
- }
- return JSONObject.toBean(jsonObject, clazz);
- }
- /**
- * 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
- * {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
- * beansList:[{}, {}, ...]}
- * @param jsonString
- * @param clazz
- * @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
- * @return
- */
- public static Object getDTO(String jsonString, Class clazz, Map map){
- JSONObject jsonObject = null;
- try{
- setDataFormat2JAVA();
- jsonObject = JSONObject.fromObject(jsonString);
- }catch(Exception e){
- e.printStackTrace();
- }
- return JSONObject.toBean(jsonObject, clazz, map);
- }
- /**
- * 从一个JSON数组得到一个java对象数组,形如:
- * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
- * @param object
- * @param clazz
- * @return
- */
- public static Object[] getDTOArray(String jsonString, Class clazz){
- setDataFormat2JAVA();
- JSONArray array = JSONArray.fromObject(jsonString);
- Object[] obj = new Object[array.size()];
- for(int i = 0; i < array.size(); i++){
- JSONObject jsonObject = array.getJSONObject(i);
- obj[i] = JSONObject.toBean(jsonObject, clazz);
- }
- return obj;
- }
- /**
- * 从一个JSON数组得到一个java对象数组,形如:
- * [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
- * @param object
- * @param clazz
- * @param map
- * @return
- */
- public static Object[] getDTOArray(String jsonString, Class clazz, Map map){
- setDataFormat2JAVA();
- JSONArray array = JSONArray.fromObject(jsonString);
- Object[] obj = new Object[array.size()];
- for(int i = 0; i < array.size(); i++){
- JSONObject jsonObject = array.getJSONObject(i);
- obj[i] = JSONObject.toBean(jsonObject, clazz, map);
- }
- return obj;
- }
- /**
- * 从一个JSON数组得到一个java对象集合
- * @param object
- * @param clazz
- * @return
- */
- public static List getDTOList(String jsonString, Class clazz){
- setDataFormat2JAVA();
- JSONArray array = JSONArray.fromObject(jsonString);
- List list = new ArrayList();
- for(Iterator iter = array.iterator(); iter.hasNext();){
- JSONObject jsonObject = (JSONObject)iter.next();
- list.add(JSONObject.toBean(jsonObject, clazz));
- }
- return list;
- }
- /**
- * 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
- * @param object
- * @param clazz
- * @param map 集合属性的类型
- * @return
- */
- public static List getDTOList(String jsonString, Class clazz, Map map){
- setDataFormat2JAVA();
- JSONArray array = JSONArray.fromObject(jsonString);
- List list = new ArrayList();
- for(Iterator iter = array.iterator(); iter.hasNext();){
- JSONObject jsonObject = (JSONObject)iter.next();
- list.add(JSONObject.toBean(jsonObject, clazz, map));
- }
- return list;
- }
- /**
- * 从json HASH表达式中获取一个map,该map支持嵌套功能
- * 形如:{"id" : "johncon", "name" : "小强"}
- * 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
- * @param object
- * @return
- */
- public static Map getMapFromJson(String jsonString) {
- setDataFormat2JAVA();
- JSONObject jsonObject = JSONObject.fromObject(jsonString);
- Map map = new HashMap();
- for(Iterator iter = jsonObject.keys(); iter.hasNext();){
- String key = (String)iter.next();
- map.put(key, jsonObject.get(key));
- }
- return map;
- }
- /**
- * 从json数组中得到相应java数组
- * json形如:["123", "456"]
- * @param jsonString
- * @return
- */
- public static Object[] getObjectArrayFromJson(String jsonString) {
- JSONArray jsonArray = JSONArray.fromObject(jsonString);
- return jsonArray.toArray();
- }
- /**
- * 把数据对象转换成json字符串
- * DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
- * 数组对象形如:[{}, {}, {}, ...]
- * map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
- * @param object
- * @return
- */
- public static String getJSONString(Object object) throws Exception{
- String jsonString = null;
- //日期值处理器
- JsonConfig jsonConfig = new JsonConfig();
- jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
- if(object != null){
- if(object instanceof Collection || object instanceof Object[]){
- jsonString = JSONArray.fromObject(object, jsonConfig).toString();
- }else{
- jsonString = JSONObject.fromObject(object, jsonConfig).toString();
- }
- }
- return jsonString == null ? "{}" : jsonString;
- }
- json-lib的使用
- 使用json-lib
- json-lib使用实例
- JSON-LIB 使用心得
- JSON-Lib的使用
- JSON – lib使用
- json-lib使用
- JSON-lib 使用
- json-lib使用
- JSON - LIB使用
- json lib的使用
- Json-lib简单使用
- json-lib的使用
- json-lib的使用
- JSON-LIB使用
- Json-lib的使用
- json-lib使用
- json-lib使用
- 【Android】webView 使用 系统自带搜索对话框问题
- 一些经典的计算机书籍
- Nginx常见问题汇总
- 毕业后五年决定命运~~~重温一下
- HDOJ: 1272 小希的迷宫
- JSON-lib 使用
- Java游戏源码大礼包
- mysql的 关键字 的原理以及优化思路
- shell 判断读入字符是回车键还是方向键、tab键
- 学习GNU Emacs命令速查表(一)
- JSON javascript 使用
- MAC OS X终端常用命令
- 2012 TCO Algorithm - Round 1A
- 大三下已逝二月