Java构造和解析Json数据的两种方法详解一——json-lib

时间:2021-09-15 23:26:49

转自:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html

www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别。下面首先介绍用json-lib构造和解析Json数据的方法示例。

用org.son构造和解析Json数据的方法详解请参见我下一篇博文:Java构造和解析Json数据的两种方法详解二

一、介绍

JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json数据,在其官网http://www.json.org/上有详细讲解,有兴趣的可以去研究。

二、下载jar依赖包:可以去这里下载

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPsAAACICAIAAACupgqeAAAOGElEQVR4nO1d3XLbxhXelwoHeIM+hiQGL9EZXaVD2bJDehI3zUWdxozrtK4kk2JQ05TtOIndTqWEHksyh+VMx27rdBLHPxJ/QXCX0wuAwC6wCyz/IIp7vjkXwO7Zs2ehj4sFuB+FMACgEtBZJwAAJApgPEAtAOMBagEYD1ALwHiAWgDGA9QCh/EPxXjw4MHe3l7yWQIAswKf8XgwwHhACCYED4dkOCQYD4ZDsr+/X6lUKnuV5BMFAGYCDuO//vrrwcAOk344JI8fP+52u+Vy+f79+8nnCgBMDw7j79+/b9t9LukrlUq5XC6Xy5XKMk3z9ayOjNKZdKtn6xjjknEW/QuSWXJwGH/v3j27b31Tre/Xnh/UXhzUXuw/e35Qe7Ffe/7tk3/+78cfXzx/DoyfUbcTMb5kIMRpUM/qiEWIwiVDVKMy4yuVSt/qPTw4dk5t2373tz/Yto0xfnhw7Ez25XI50TTniyQZz+1LnvH1rI6QbhixCXP6oUhdMngfGSXAYXy5XLZ63Tvf/WBZVq/X++Xu5qtHn797smtZ1p3vfgDGz6GvcVc18QkLCO8VndFtbQHAYbxpmr1ud7vyqNlsnpyc/GxmXv32V69fv3779u125ZGzpjdNkxfNu2l6N0i/hJpUSgbSsyX3HmyUfC+qVbRDZORs0Jm63XP/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SyFjq0eaDo+RfuF4VFUwkJZ74js3LmhoteB4em6NC5pz7DT3IpirTlg/vtc8HI1u6NpWGqG0GcxWrtZ1cJNn43v5BEbnnzYuaGg1bSA3bdoCQ+B2F76wpNkjTy7590/e1QCLMbSer3bt4WmXeCap7KabnHbJ6VEuhYwtv6SxoSGE9I0jcuqQgKnSN47IaddccRyYwoCZK05YP35jQ0MrBaYj6pSxrTWE1kxuVWwtlQAvMS+fwOj808aGhpCmpzjNI3IOded1dJRLIZTabLBXO2r4YGFD6/lqpz886RLPJJXddJOTLjk5yqWQ8ZcuOTnKbRScQvePcdI1VxBKbTZcz4KBtFy165TrG0ees3dMm7kyCuvGd5i0ZjoO1U0dueUBa2xoVKd+kk4vkbVHuRQd381WMN6uuYKcYY5yc/1Hwz/KpXyHwJUZhQp3RyfjdORfN3rUbCiwOHMZ/65DPJNUdtNN3nXIu8Pce8i41SHvOuSW/1BpjhzMFerJ9ZZfqGcOneNGRvOOaTNXHH8/fiOjoZU1IxSNtdsBVbmeOXSC6JnDuNqOQ6nI+NR4qWh6ZtNAWq7q5XmbHr5xq9PIaEzHjgOnOy8ZduAO3lszmKtxm5chGM/Qer7atoZvO8QzSWU33eRth7wd/WGC5ctqiY1XtQs7Z3MZ/6ZNPJNUdtNN3rTJ9xd1lMp9zxYusSU2XtUu7LwNreerLWv4uk08k1R2001etwlCxp/YkqW1HVfEPffxJtaRSobW89WmNfylRcDAVDC08eenP5/Yr9tnnwoYWAKGdv/+76/2X75pD5q9IRjY0hvqWvb2oxcffPn0zCW3YGAJGPx6B0AtAOMBagEYD1ALwHiAWvg/tMEeYU2n9zwAAAAASUVORK5CYII=" alt="" />

三、基本方法介绍

1. List集合转换成json方法

List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );

2. Map集合转换成json方法

Java构造和解析Json数据的两种方法详解一——json-lib
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);
Java构造和解析Json数据的两种方法详解一——json-lib

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代码

Java构造和解析Json数据的两种方法详解一——json-lib
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);
Java构造和解析Json数据的两种方法详解一——json-lib

四、演示示例

这里以基本的几个常用方法进行测试

Java构造和解析Json数据的两种方法详解一——json-lib
package com.json;

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; /**
* 使用json-lib构造和解析Json数据
*
* @author Alexia
* @date 2013/5/23
*
*/
public class JsonTest { /**
* 构造Json数据
*
* @return
*/
public static String BuildJson() { // JSON格式数据解析对象
JSONObject jo = new JSONObject(); // 下面构造两个map、一个list和一个Employee对象
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "Alexia");
map1.put("sex", "female");
map1.put("age", "23"); Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Edward");
map2.put("sex", "male");
map2.put("age", "24"); List<Map> list = new ArrayList<Map>();
list.add(map1);
list.add(map2); Employee employee = new Employee();
employee.setName("wjl");
employee.setSex("female");
employee.setAge(24); // 将Map转换为JSONArray数据
JSONArray ja1 = JSONArray.fromObject(map1);
// 将List转换为JSONArray数据
JSONArray ja2 = JSONArray.fromObject(list);
// 将Bean转换为JSONArray数据
JSONArray ja3 = JSONArray.fromObject(employee); System.out.println("JSONArray对象数据格式:");
System.out.println(ja1.toString());
System.out.println(ja2.toString());
System.out.println(ja3.toString()); // 构造Json数据,包括一个map和一个Employee对象
jo.put("map", ja1);
jo.put("employee", ja2);
System.out.println("\n最终构造的JSON数据格式:");
System.out.println(jo.toString()); return jo.toString(); } /**
* 解析Json数据
*
* @param jsonString Json数据字符串
*/
public static void ParseJson(String jsonString) { // 以employee为例解析,map类似
JSONObject jb = JSONObject.fromObject(jsonString);
JSONArray ja = jb.getJSONArray("employee"); List<Employee> empList = new ArrayList<Employee>(); // 循环添加Employee对象(可能有多个)
for (int i = 0; i < ja.size(); i++) {
Employee employee = new Employee(); employee.setName(ja.getJSONObject(i).getString("name"));
employee.setSex(ja.getJSONObject(i).getString("sex"));
employee.setAge(ja.getJSONObject(i).getInt("age")); empList.add(employee);
} System.out.println("\n将Json数据转换为Employee对象:");
for (int i = 0; i < empList.size(); i++) {
Employee emp = empList.get(i);
System.out.println("name: " + emp.getName() + " sex: "
+ emp.getSex() + " age: " + emp.getAge());
} } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub ParseJson(BuildJson());
} }
Java构造和解析Json数据的两种方法详解一——json-lib

运行结果如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNEAAADSCAIAAADrFcGUAAAgAElEQVR4nO3d25miMAAGUOuiIOqhGpqhGPYBgQSSAA7Rcfach/0cwBDCZflNlMcIAAAAdTw+XQEAAAD+LJkz8ngkGiQ58eTb7y3w/BuXhS+9BQAA4F4CSSQZGpOS7yq8yC1/suRCaYfbInYCAACf8j+mkR9mvMLEfdfiYeYsF77/M1ntJFETAAD4uP89liwpcR9Ecz2c++B3clbyz2R99mWO+Sh7pp77FQEAALzBYRTp2yi5tP04juPYNfOEpmnnifH0LpzSdMM4jsMyu+mikueF3+9xpZ/z8M9Nuntk+jNzwXIfDjcl5JLqmU0DAAB4v7OxZOiaJVn27TNDjs8YGQTReaGha+LYGbw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vZXUqb8+NhYN+Ku4yF+V5TT16NW5gQAgI86ypzLLfvmdWDNFW0/Bnf8z3ckvoj5Yg6I+znXqVFkmgZVjuM4jlOFumZKfcEGJOufLK1YfrGmu+LL7fCc+4NRrrn2L2/vPnO+sL+Kx8Na6r6T82fHQ7D2aAvi6nzxLyQBAMAfcD1zxn1K6yKFzHnTFyDj73OuU3OZsG+bbujbaThsP0egXP2TpRXLP1vpafniG5cg9npAKmX+/PYmM+elDTw6HrYz7jsexlTH8Ha2fk4AAPiolzJn/F3Eg8yZGs+5euX7nJs/o8nhWNaxb9u2bbphHhK7jhJO1H9+yy715cvPiELckjnz7RD+/uo8yjacd659zmT+VERLja290vOYLz8sOJxx4/EQ7IvU92xlTgAA+LSj3xCa7v/D1/FPxmx+tebR9s+fs5l/iWf7jkec2U5njMSQzPV7l+FQ1nb9yuWwJrjo66j7+u8r+dgEzVT5GXFNNzF52w7D2nKpB2Ze+g2hTPuf3N5gLfn9lZIpP9pnTdNkGvTF4yFR/+3Xd5fJxtYCAMDHfNOzUvhi6YHRAADAHydz8g7hj/8CAAD/D5mTeoLhrzo5AQDgvyRzAgAAUIvMCQAAQC0yJwAAALXInAAAANQicwIAAFCLzAkAAEAtMicAAAC1yJwAAADUInMCAABQi8wJAABALTInAAAAtcicAAAA1CJzAgAAUMs/EaYZcp4ZpkEAAAAASUVORK5CYII=" alt="" />

五、与org.json比较

json-lib和org.json的使用几乎是相同的,我总结出的区别有两点:

1. org.json比json-lib要轻量得多,前者没有依赖任何其他jar包,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等组件

2. json-lib在构造bean和解析bean时比org.json要方便的多,json-lib可直接与bean互相转换,而org.json不能直接与bean相互转换而需要map作为中转,若将bean转为json数据,首先需要先将bean转换为map再将map转为json,比较麻烦。

总之,还是那句话—适合自己的才是最好的,大家要按需选取使用哪种方法进行解析。最后给大家介绍两款解析Json数据的工具:一是在线工具JSONEdit(http://braincast.nl/samples/jsoneditor/);另一个是Eclipse的插件JSON Tree Analyzer,都很好用,推荐给大家使用!