java构建json_Java构造和解析Json数据的两种方法详解一
在www.json.org上公布了很多JAVA下的json構(gòu)造和解析工具,其中org.json和json-lib比較簡(jiǎn)單,兩者使用上差不多但還是有些區(qū)別。下面首先介紹用json-lib構(gòu)造和解析Json數(shù)據(jù)的方法示例。
用org.son構(gòu)造和解析Json數(shù)據(jù)的方法詳解請(qǐng)參見我下一篇博文:Java構(gòu)造和解析Json數(shù)據(jù)的兩種方法詳解二
一、介紹
JSON-lib包是一個(gè)beans,collections,maps,java arrays 和XML和JSON互相轉(zhuǎn)換的包,主要就是用來解析Json數(shù)據(jù),在其官網(wǎng)http://www.json.org/上有詳細(xì)講解,有興趣的可以去研究。
二、下載jar依賴包:可以去這里下載
三、基本方法介紹
1. List集合轉(zhuǎn)換成json方法
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
2. Map集合轉(zhuǎn)換成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轉(zhuǎn)換成json代碼
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4. 數(shù)組轉(zhuǎn)換成json代碼
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5.?一般數(shù)據(jù)轉(zhuǎn)換成json代碼
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6. beans轉(zhuǎn)換成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);
四、演示示例
這里以基本的幾個(gè)常用方法進(jìn)行測(cè)試
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構(gòu)造和解析Json數(shù)據(jù)
*
* @author Alexia
* @date 2013/5/23
*
*/
public class JsonTest {
/**
* 構(gòu)造Json數(shù)據(jù)
*
* @return
*/
public static String BuildJson() {
// JSON格式數(shù)據(jù)解析對(duì)象
JSONObject jo = new JSONObject();
// 下面構(gòu)造兩個(gè)map、一個(gè)list和一個(gè)Employee對(duì)象
Map map1 = new HashMap();
map1.put("name", "Alexia");
map1.put("sex", "female");
map1.put("age", "23");
Map map2 = new HashMap();
map2.put("name", "Edward");
map2.put("sex", "male");
map2.put("age", "24");
List list = new ArrayList();
list.add(map1);
list.add(map2);
Employee employee = new Employee();
employee.setName("wjl");
employee.setSex("female");
employee.setAge(24);
// 將Map轉(zhuǎn)換為JSONArray數(shù)據(jù)
JSONArray ja1 = JSONArray.fromObject(map1);
// 將List轉(zhuǎn)換為JSONArray數(shù)據(jù)
JSONArray ja2 = JSONArray.fromObject(list);
// 將Bean轉(zhuǎn)換為JSONArray數(shù)據(jù)
JSONArray ja3 = JSONArray.fromObject(employee);
System.out.println("JSONArray對(duì)象數(shù)據(jù)格式:");
System.out.println(ja1.toString());
System.out.println(ja2.toString());
System.out.println(ja3.toString());
// 構(gòu)造Json數(shù)據(jù),包括一個(gè)map和一個(gè)Employee對(duì)象
jo.put("map", ja1);
jo.put("employee", ja2);
System.out.println("\n最終構(gòu)造的JSON數(shù)據(jù)格式:");
System.out.println(jo.toString());
return jo.toString();
}
/**
* 解析Json數(shù)據(jù)
*
* @param jsonString Json數(shù)據(jù)字符串
*/
public static void ParseJson(String jsonString) {
// 以employee為例解析,map類似
JSONObject jb = JSONObject.fromObject(jsonString);
JSONArray ja = jb.getJSONArray("employee");
List empList = new ArrayList();
// 循環(huán)添加Employee對(duì)象(可能有多個(gè))
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數(shù)據(jù)轉(zhuǎn)換為Employee對(duì)象:");
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());
}
}
運(yùn)行結(jié)果如下
五、與org.json比較
json-lib和org.json的使用幾乎是相同的,我總結(jié)出的區(qū)別有兩點(diǎn):
1. org.json比json-lib要輕量得多,前者沒有依賴任何其他jar包,而后者要依賴ezmorph和commons的lang、logging、beanutils、collections等組件
2. json-lib在構(gòu)造bean和解析bean時(shí)比org.json要方便的多,json-lib可直接與bean互相轉(zhuǎn)換,而org.json不能直接與bean相互轉(zhuǎn)換而需要map作為中轉(zhuǎn),若將bean轉(zhuǎn)為json數(shù)據(jù),首先需要先將bean轉(zhuǎn)換為map再將map轉(zhuǎn)為json,比較麻煩。
總之,還是那句話—適合自己的才是最好的,大家要按需選取使用哪種方法進(jìn)行解析。最后給大家介紹兩款解析Json數(shù)據(jù)的工具:一是在線工具JSON?Edit(http://braincast.nl/samples/jsoneditor/);另一個(gè)是Eclipse的插件JSON Tree Analyzer,都很好用,推薦給大家使用!
from:https://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html
總結(jié)
以上是生活随笔為你收集整理的java构建json_Java构造和解析Json数据的两种方法详解一的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 实时屏幕监控 linux面板,分享|LX
- 下一篇: 卷的作用_还在盲目的制作蛋糕卷吗?先来搞
