json-lib包笔记

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]; }}

测试类:

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import net.sf.json.JSONArray;
  6. import net.sf.json.JSONObject;
  7. public class Json {
  8. public static void main(String[] args) {
  9. Json j = new Json();
  10. j.bean2json();
  11. }
  12. public void arr2json() {
  13. boolean[] boolArray = new boolean[] { truefalsetrue };
  14. JSONArray jsonArray = JSONArray.fromObject(boolArray);
  15. System.out.println(jsonArray);
  16. // prints [true,false,true]
  17. }
  18. public void list2json() {
  19. List list = new ArrayList();
  20. list.add("first");
  21. list.add("second");
  22. JSONArray jsonArray = JSONArray.fromObject(list);
  23. System.out.println(jsonArray);
  24. // prints ["first","second"]
  25. }
  26. public void createJson() {
  27. JSONArray jsonArray = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]");
  28. System.out.println(jsonArray);
  29. // prints ["json","is","easy"]
  30. }
  31. public void map2json() {
  32. Map
  33. map.put("name", "json");
  34. map.put("bool", Boolean.TRUE);
  35. map.put("int", new Integer(1));
  36. map.put("arr", new String[] { "a", "b" });
  37. map.put("func", "function(i){ return this.arr[i]; }");
  38. JSONObject json = JSONObject.fromObject(map);
  39. System.out.println(json);
  40. // prints
  41. // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
  42. // return this.arr[i]; }]
  43. }
  44. public void bean2json() {
  45. JSONObject jsonObject = JSONObject.fromObject(new MyBean());
  46. System.out.println(jsonObject);
  47. /*
  48. * prints
  49. * {"func1":function(i){ return this.options[i];
  50. * },"pojoId":1,"name":"json","func2":function(i){ return
  51. * this.options[i]; }}
  52. */
  53. }
  54. public void json2bean() {
  55. String json = "{name=/"json2/",func1:true,pojoId:1,func2:function(a){ return a; },options:[‘1‘,‘2‘]}";
  56. JSONObject jb = JSONObject.fromString(json);
  57. JSONObject.toBean(jb, MyBean.class);
  58. System.out.println();
  59. }
  60. }

操作的bean:

  1. import net.sf.json.JSONFunction;
  2. public class MyBean {
  3. private String name = "json";
  4. private int pojoId = 1;
  5. // private char[] options = new char[] { ‘a‘, ‘f‘ };
  6. private String func1 = "function(i){ return this.options[i]; }";
  7. private JSONFunction func2 = new JSONFunction(new String[] { "i" },
  8. "return this.options[i];");
  9. // getters & setters
  10. ......
  11. }

使用JSON的方法

JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互。本文将快速讲解 JSON 格式,并通过代码示例演示如何分别在客户端和服务器端进行 JSON 格式数据的处理。

Json必需的包

commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.2.3-jdk13.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar

以上包可以从

http://commons.apache.org/index.html

http://json-lib.sourceforge.net/

http://ezmorph.sourceforge.net/

http://morph.sourceforge.net/

http://www.docjar.com/

中下载到。

出现java.lang.NoClassDefFoundError: net/sf/ezmorph/Morpher错误是因为没有导入ezmorph.jar文件或版本不对。

出现java.lang.NoClassDefFoundError: org/apache/commons/collections/map/ListOrderedMap错误是因为没有导入commons-collections.jar文件或版本不对。

Java代码转换成json代码

1.       List集合转换成json代码


List list = new ArrayList();

list.add( "first" );

list.add( "second" );

JSONArray jsonArray2 = JSONArray.fromObject( list );

2.       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 json = JSONObject.fromObject(map);

3.       Bean转换成json代码


JSONObject jsonObject = JSONObject.fromObject(new JsonBean());

4.       数组转换成json代码


boolean[] boolArray = new boolean[] { truefalsetrue };

JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

5. 一般数据转换成json代码


JSONArray jsonArray3 = JSONArray.fromObject("[‘json‘,‘is‘,‘easy‘]" );

6.       beans转换成json代码


List list = new ArrayList();

JsonBean2 jb1 = new JsonBean2();

jb1.setCol(1);

jb1.setRow(1);

jb1.setValue("xx");

JsonBean2 jb2 = new JsonBean2();

jb2.setCol(2);

jb2.setRow(2);

jb2.setValue("");

list.add(jb1);

list.add(jb2);

JSONArray ja = JSONArray.fromObject(list);

时间: 2024-08-08 19:29:59

json-lib包笔记的相关文章

CoAP学习笔记——nodeJS node-coap返回JSON数据包

0 前言 本文说明如何使用node-coap返回JSON数据包.CoAP是专门为物联网系统开发的面向网络的应用层协议栈,CoAP建立在UDP协议之上尽可能减少网络开销,又具有HTTP Restful类型的特性.node-coap使用nodejs实现了coap的客户端和服务器端. [测试环境]--ubuntu/Linux [相关博文] [CoAP协议文档--The Constrained Application Protocol (CoAP)] [CoAP协议学习--CoAP基础] [CoAP学习

Json.Net学习笔记

http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http://www.cnblogs.com/freshman0216/p/4161800.html http://www.cnblogs.com/xwgli/archive/2013/08/30/3290964.html

pushlet服务端推送——点对点单播(不用修改lib包)

pushlet点对点单播,在网上看 ,大家都是将包修改然后替换lib中class,其实不用不这么麻烦,java可以继承嘛,继承原来的类重写里面的方法就行,不必编译出来替换class,这样不方便修改 新建一个类,继承nl.justobjects.pushlet.core.SessionManager类,重写里面的createSession方法即可 package com.pushlet.serveToClient; import javax.servlet.http.HttpSession; im

json解包与json封包

首先,对两个名词进行简单的说明: 1.NSData 用来存储二进制的数据类型.NSData类提供了一种简单的方式,它用来设置缓冲区.将文件的内容读入缓冲区,或将缓冲区的内容写到一个文件.不变缓冲区(NSData类),也可定义可变的缓冲区(NSMutableData类). 2.json 作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式. 小结:我们只需要明白NSData类型是用来存储二进制数据的,json是一种数据格式,注意是格式. 接着,先用一段文字简单描述一下json解

观django-messages包笔记

django_messages是一个提供注册用户之间互相发送消息的django app.最近在研究其实现机制,安装测试非常容易,导入包,配好url以及syncdb生成数据库即可使用. 一.收获一: 我们在setting里设置好AUTH_USER_MODEL = 'accounts.User', 然后就可以在数据库中: from django.conf import settings AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'a

前端学习——使用Ajax方式POST JSON数据包

0.前言 本文解释如何使用Jquery中的ajax方法传递JSON数据包,传递的方法使用POST(当然PUT又有时也是一个不错的选择).POST JSON数据包相比标准的POST格式可读性更好些,层次结构也更清晰. 为了说明问题,前端和后端较为简单,重点突出AJAX的应用. [前端]--add-post-json.html 图1 add页面 [后端]--add-post-json.php <?php // 返回JSON格式 header('Content-Type:application/jso

AndroidStudio 导入imageloader项目lib包,错误处理

使用AndroidStudio时候导入开源项目ImageLoader lib包时,出现错误 Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.internal.LoggedErrorException: Failed to run command: E:\android_studio\sdk_studio\android-sdk-windows\build-tools\21.1.1\dx.bat -

jar包与lib包的区别

jar包是编译时使用,假如编译出错代码没问题一定是jar包的问题,lib是运行时使用,比如程序启动后出错了但是编译没有问题,就可能是lib出错了,不会是jar包的问题.jar包与lib包的区别

c# 生成json数据包

json数据类型,归根到底就是一个字符串,管他里面什么格式,它就是一个字符串来的! 看一个json数据包: { "touser":"OPENID", "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", "url":"http://weixin.qq.com/download", "topcolor"

sde库缺少lib包解决方法

缺少lib包解决方法: 10.0                               配置ST_Geometry SDE安装后的类库: 修改listener.ora文件 修改tnsnames.ora文件 修改oracle用户的.profile文件,加入SDEHOME/lib路径 监听重启前状态(动态监听): 重启监听,重启后为静态监听 验证配置是否成功,打开pl/sql,输入select sde.st_astext(shape) from china进行测试. 配置成功.   10.1