JSON学习(转)
来源:互联网 发布:同企数据 类似的网站 编辑:程序博客网 时间:2024/05/16 07:06
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]; }}
- 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();
- }
- }
- 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的开发包,提出来,大家探讨一下~!!!!
- JSON学习(转)
- JSON学习(一)
- JSON学习(一)
- JSON学习(一)
- JSON学习(二)
- Json学习(三):FastJson操作Json
- JSON学习笔记(四)- JSON.parse()
- JSON学习笔记(五)- JSON.stringify()
- json 学习之(json基础)
- JSON--JSON学习入门(了解什么是JSON)
- Json学习(一):json简介与JS操作Json
- json学习(java,js)
- JSON学习笔记(一)
- JS学习17(JSON)
- Json学习笔记(三)-前端json字符串互转(4种转换方式)
- json学习-什么是json
- 【json】慕课网json学习
- 【框架学习】springMVC转json输出(fastjson)
- 怎样降低被套股的成本
- 乱码解决方案JNI 数据类型转换
- 给对话框程序制作启动画面
- 自己的一些缺点
- 你知道Google、 百度、谷歌的最本质区别吗
- JSON学习(转)
- java 类的序列化和持久化理解
- 项目经理修炼手册 1.2 万丈高楼平地起,当管理者需要知道那些事
- ABCD
- CU上的老帖(无名师的Unix心传)
- 关于FIELD-GROUPS的使用
- VB命名规则及代码规范
- linux初学之琐碎知识点整理二
- 生单词