JSON的使用方法
来源:互联网 发布:java 类 继承 编辑:程序博客网 时间:2024/04/24 22:32
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[] { true, false, true };
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);
- JSON的使用方法
- json 的使用方法
- json数据的使用方法
- SuperObject的JSON使用方法
- JSON的使用方法
- JSON 的使用方法
- json基本的使用方法
- JSON的使用方法
- JSON.parse()和JSON.stringify()的使用方法
- fast-json.jar的使用方法
- fast-json.jar的使用方法
- JSON库LitJSON的使用方法
- fast-json.jar的使用方法
- Json数据格式的使用方法入门教程
- Qt中json的使用方法
- Unity中json的使用方法
- JSON-FastJson的基本使用方法
- python 中json的使用方法
- BF52x学习
- 更新内容:关于Windows Azure技术内容搜索的新页面可用
- Scrum
- CONCAT_WS函数
- ASP遍历数据库
- JSON的使用方法
- Web浏览器大比拼
- visual studio快捷键
- device 与device_driver之间的绑定
- awk
- hdu 1233 还是畅通工程(最小生成树)
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)
- devices driver module
- discriminator-value