使用Java构造和解析Json数据的两种方法(详解一)
JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式。同时,JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不须要任何特殊的API或工具包。
在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别。下面首先介绍用json-lib构造和解析Json数据的方法示例。
用org.son构造和解析Json数据的方法详解请参见我下一篇博文:使用Java构造和解析Json数据的两种方法(详解二)
一、介绍
JSON-lib包是一个beans,collections,maps,javaarrays和XML和JSON互相转换的包,主要就是用来解析Json数据,在其官网http://www.json.org/上有详细讲解,有兴趣的可以去研究。
二、下载jar依赖包:可以去这里下载
三、基本方法介绍
1.List集合转换成json方法
Listlist=newArrayList(); list.add("first"); list.add("second"); JSONArrayjsonArray2=JSONArray.fromObject(list);
2.Map集合转换成json方法
Mapmap=newHashMap(); map.put("name","json"); map.put("bool",Boolean.TRUE); map.put("int",newInteger(1)); map.put("arr",newString[]{"a","b"}); map.put("func","function(i){returnthis.arr[i];}"); JSONObjectjson=JSONObject.fromObject(map);
3.Bean转换成json代码
JSONObjectjsonObject=JSONObject.fromObject(newJsonBean());
4.数组转换成json代码
boolean[]boolArray=newboolean[]{true,false,true}; JSONArrayjsonArray1=JSONArray.fromObject(boolArray);
5.一般数据转换成json代码
JSONArrayjsonArray3=JSONArray.fromObject("['json','is','easy']");
6.beans转换成json代码
Listlist=newArrayList(); JsonBean2jb1=newJsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean2jb2=newJsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue(""); list.add(jb1); list.add(jb2); JSONArrayja=JSONArray.fromObject(list);
四、演示示例
这里以基本的几个常用方法进行测试
packagecom.json; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importnet.sf.json.JSONArray; importnet.sf.json.JSONObject; /** *使用json-lib构造和解析Json数据 * *@authorAlexia *@date2013/5/23 * */ publicclassJsonTest{ /** *构造Json数据 * *@return */ publicstaticStringBuildJson(){ //JSON格式数据解析对象 JSONObjectjo=newJSONObject(); //下面构造两个map、一个list和一个Employee对象 Map<String,String>map1=newHashMap<String,String>(); map1.put("name","Alexia"); map1.put("sex","female"); map1.put("age","23"); Map<String,String>map2=newHashMap<String,String>(); map2.put("name","Edward"); map2.put("sex","male"); map2.put("age","24"); List<Map>list=newArrayList<Map>(); list.add(map1); list.add(map2); Employeeemployee=newEmployee(); employee.setName("wjl"); employee.setSex("female"); employee.setAge(24); //将Map转换为JSONArray数据 JSONArrayja1=JSONArray.fromObject(map1); //将List转换为JSONArray数据 JSONArrayja2=JSONArray.fromObject(list); //将Bean转换为JSONArray数据 JSONArrayja3=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()); returnjo.toString(); } /** *解析Json数据 * *@paramjsonStringJson数据字符串 */ publicstaticvoidParseJson(StringjsonString){ //以employee为例解析,map类似 JSONObjectjb=JSONObject.fromObject(jsonString); JSONArrayja=jb.getJSONArray("employee"); List<Employee>empList=newArrayList<Employee>(); //循环添加Employee对象(可能有多个) for(inti=0;i<ja.size();i++){ Employeeemployee=newEmployee(); 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(inti=0;i<empList.size();i++){ Employeeemp=empList.get(i); System.out.println("name:"+emp.getName()+"sex:" +emp.getSex()+"age:"+emp.getAge()); } } /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub ParseJson(BuildJson()); } }
运行结果如下
五、与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的插件JSONTreeAnalyzer,都很好用,推荐给大家使用!
以上所述是小编给大家介绍的使用Java构造和解析Json数据的两种方法(详解一),希望对大家有所帮助!