志在指尖
用双手敲打未来

json在线解析(json解析的几种方式介绍)

json在线解析

在线解析:https://www.sojson.com/

json解析的几种方式介绍

在实际工作中,我们经常需要使用json进行数据解析,JSON是一种轻量级的数据交换格式,易于人阅读和编写,能够有效进行网络传输,那json格式在线解析操作方法是什么?下面来我们就来给大家讲解一下。
json格式在线解析很简单,只要我们找到json解析工具,将要解析的文件直接进行解析就好了,没有什么难度的!json
json如何解析?
一、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;
}
}
二、解析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;
}
}
?

未经允许不得转载:IT技术网站 » json在线解析(json解析的几种方式介绍)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载