基于java解析JSON的三种方式详解
本文实例分析了基于java解析JSON的三种方式。分享给大家供大家参考,具体如下:
一、什么是JSON?
JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。
JSON就是一串字符串只不过元素会使用特定的符号标注。
{}双括号表示对象
[]中括号表示数组
""双引号内是属性或值
:冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
所以{"name":"Michael"}可以理解为是一个包含name为Michael的对象
而[{"name":"Michael"},{"name":"Jerry"}]就表示包含两个对象的数组
当然了,你也可以使用{"name":["Michael","Jerry"]}来简化上面一部,这是一个拥有一个name数组的对象
二、JSON解析之传统的JSON解析
1、生成json字符串
publicstaticStringcreateJsonString(Stringkey,Objectvalue){ JSONObjectjsonObject=newJSONObject(); jsonObject.put(key,value); returnjsonObject.toString(); }
2、解析JSON字符串
分为以下三种情况,一个JavaBean,一个List数组,一个嵌套Map的List数组:
importjava.util.ArrayList; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.Map; importorg.json.JSONArray; importorg.json.JSONObject; importcom.android.myjson.domain.Person; /** *完成对json数据的解析 * */ publicclassJsonTools{ publicstaticPersongetPerson(Stringkey,StringjsonString){ Personperson=newPerson(); try{ JSONObjectjsonObject=newJSONObject(jsonString); JSONObjectpersonObject=jsonObject.getJSONObject("person"); person.setId(personObject.getInt("id")); person.setName(personObject.getString("name")); person.setAddress(personObject.getString("address")); }catch(Exceptione){ //TODO:handleexception } returnperson; } publicstaticListgetPersons(Stringkey,StringjsonString){ Listlist=newArrayList(); try{ JSONObjectjsonObject=newJSONObject(jsonString); //返回json的数组 JSONArrayjsonArray=jsonObject.getJSONArray(key); for(inti=0;i<jsonArray.length();i++){ JSONObjectjsonObject2=jsonArray.getJSONObject(i); Personperson=newPerson(); person.setId(jsonObject2.getInt("id")); person.setName(jsonObject2.getString("name")); person.setAddress(jsonObject2.getString("address")); list.add(person); } }catch(Exceptione){ //TODO:handleexception } returnlist; } publicstaticListgetList(Stringkey,StringjsonString){ Listlist=newArrayList(); try{ JSONObjectjsonObject=newJSONObject(jsonString); JSONArrayjsonArray=jsonObject.getJSONArray(key); for(inti=0;i<jsonArray.length();i++){ Stringmsg=jsonArray.getString(i); list.add(msg); } }catch(Exceptione){ //TODO:handleexception } returnlist; } publicstaticList>listKeyMaps(Stringkey, StringjsonString){ List>list=newArrayList>(); try{ JSONObjectjsonObject=newJSONObject(jsonString); JSONArrayjsonArray=jsonObject.getJSONArray(key); for(inti=0;i<jsonArray.length();i++){ JSONObjectjsonObject2=jsonArray.getJSONObject(i); Mapmap=newHashMap(); Iteratoriterator=jsonObject2.keys(); while(iterator.hasNext()){ Stringjson_key=iterator.next(); Objectjson_value=jsonObject2.get(json_key); if(json_value==null){ json_value=""; } map.put(json_key,json_value); } list.add(map); } }catch(Exceptione){ //TODO:handleexception } returnlist; } }
三、JSON解析之GSON
1、生成JSON字符串
importcom.google.gson.Gson; publicclassJsonUtils{ publicstaticStringcreateJsonObject(Objectobj){ Gsongson=newGson(); Stringstr=gson.toJson(obj); returnstr; } }
2、解析JSON
importjava.util.ArrayList; importjava.util.List; importjava.util.Map; importcom.google.gson.Gson; importcom.google.gson.reflect.TypeToken; ; publicclassGsonTools{ publicGsonTools(){ //TODOAuto-generatedconstructorstub } /** *@param *@paramjsonString *@paramcls *@return */ publicstaticTgetPerson(StringjsonString,Classcls){ Tt=null; try{ Gsongson=newGson(); t=gson.fromJson(jsonString,cls); }catch(Exceptione){ //TODO:handleexception } returnt; } /** *使用Gson进行解析List * *@param *@paramjsonString *@paramcls *@return */ publicstaticListgetPersons(StringjsonString,Classcls){ Listlist=newArrayList(); try{ Gsongson=newGson(); list=gson.fromJson(jsonString,newTypeToken>(){ }.getType()); }catch(Exceptione){ } returnlist; } /** *@paramjsonString *@return */ publicstaticListgetList(StringjsonString){ Listlist=newArrayList(); try{ Gsongson=newGson(); list=gson.fromJson(jsonString,newTypeToken>(){ }.getType()); }catch(Exceptione){ //TODO:handleexception } returnlist; } publicstaticList>listKeyMaps(StringjsonString){ List>list=newArrayList>(); try{ Gsongson=newGson(); list=gson.fromJson(jsonString, newTypeToken>>(){ }.getType()); }catch(Exceptione){ //TODO:handleexception } returnlist; } }
四、JSON解析之FastJSON
importjava.util.ArrayList; importjava.util.List; importjava.util.Map; importcom.alibaba.fastjson.JSON; importcom.alibaba.fastjson.TypeReference; publicclassJsonTool{ publicstaticTgetPerson(Stringjsonstring,Classcls){ Tt=null; try{ t=JSON.parseObject(jsonstring,cls); }catch(Exceptione){ //TODO:handleexception } returnt; } publicstaticListgetPersonList(Stringjsonstring,Classcls){ Listlist=newArrayList(); try{ list=JSON.parseArray(jsonstring,cls); }catch(Exceptione){ //TODO:handleexception } returnlist; } publicstaticList>getPersonListMap1( Stringjsonstring){ List>list=newArrayList>(); try{ list=JSON.parseObject(jsonstring, newTypeReference>>(){ }.getType()); }catch(Exceptione){ //TODO:handleexception } returnlist; } }
总结:
JSON对于移动设备来说,尤其对于网络环境较差和流量限制的情况下,相对于XML格式的数据传输会更节省流量,传输效率更高。在这三种解析方式中FastJson是效率最高的,推荐使用。
在线