JSON文档解析 Google提供的GSON的jar包,利用JSONReader方法
2013-07-25 20:39
525 查看
下载地址:http://download-vdisk.sina.com.cn/38697621/1e8d308e075683d8f963cf5ea6aef7b9d3763cb2?ip=1374760440,10.75.7.213&ssig=iMtArsXQCn&Expires=1374760440&KID=sae,l30zoo1wmz&fn=gson-2.2.4--JAR%E5%8C%85.zip
解析的文档内容为:
{
"responseCode": 0,
"msgInfo": "业务处理成功。",
"msgDetailInfo": null,
"data": [
{
"pbtId": 1,
"pbtName": "公积金",
"keyGroupList": [
{
"keyGroupId": 1,
"itemId": 10001,
"keyList": [
{
"keyValueId": 1,
"cityCode": "3501",
"cityName": "福州",
"keyName": "housFundAccount",
"keyText": "账号",
"keyDesc": "公积金的账号",
"keyValue": "350104198001019999",
"isPrivate": "0"
},
{
"keyValueId": 2,
"cityCode": "3501",
"cityName": "福州",
"keyName": "housFundPassword",
"keyText": "密码",
"keyDesc": "公积金的密码",
"keyValue": "9999",
"isPrivate": "1"
}
]
},
{
"keyGroupId": 2,
"itemId": 10002,
"keyList": [
{
"keyValueId": 3,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "housFundAccount",
"keyText": "账号",
"keyDesc": "公积金的账号",
"keyValue": "350204198001018888",
"isPrivate": "0"
},
{
"keyValueId": 4,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "housFundPassword",
"keyText": "密码",
"keyDesc": "公积金的密码",
"keyValue": "8888",
"isPrivate": "1"
}
]
}
]
},
{
"pbtId": 2,
"pbtName": "医保",
"keyGroupList": [
{
"keyGroupId": 3,
"itemId": 10003,
"keyList": [
{
"keyValueId": 5,
"cityCode": "3501",
"cityName": "福州",
"keyName": "mediInsuranceAccount",
"keyText": "账号",
"keyDesc": "医保的账号",
"keyValue": "350104198001019999",
"isPrivate": "0"
},
{
"keyValueId": 6,
"cityCode": "3501",
"cityName": "福州",
"keyName": "mediInsurancePassword",
"keyText": "密码",
"keyDesc": "医保的密码",
"keyValue": "111111",
"isPrivate": "1"
}
]
},
{
"keyGroupId": 4,
"itemId": 10004,
"keyList": [
{
"keyValueId": 7,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "mediInsuranceAccount",
"keyText": "账号",
"keyDesc": "医保的账号",
"keyValue": "350204198001018888",
"isPrivate": "0"
},
{
"keyValueId": 8,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "mediInsurancePassword",
"keyText": "密码",
"keyDesc": "医保的密码",
"keyValue": "000000",
"isPrivate": "1"
}
]
}
]
}
]
}
解析步骤:
第一步:创建不同的javaBean用于存储每一层的属性值;
第二步:若是JSON文档存放在Assets中,获取文档的内容存在在String中
/**
* 获取文件流
* @param context
* @return
* @throws IOException
*/
public InputStream getInputStream(Context context,String fileName) throws IOException{
AssetManager am=context.getAssets();
InputStream is=am.open(fileName);
return is;
}
/**
* 将文件中的内容,保存在String中
* @param is
* @return
* @throws IOException
*/
public String getFileData(InputStream is) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
String str;
while((str=br.readLine())!=null){
sb.append(str);
}
return sb.toString();
}
/**
* 获取文件资源内容
* @param context
* @param fileName
* @return
* @throws IOException
*/
public String getFileData(Context context,String fileName) throws IOException{
FileUtil fileUtil=new FileUtil();
InputStream is=fileUtil.getInputStream(context, fileName);
return fileUtil.getFileData(is);
}
第三步:解析文件
private ArrayList<Business> arrayListBusiness = new ArrayList<Business>();
private ArrayList<Datas> arrayListDatas = new ArrayList<Datas>();
private ArrayList<KeyGroupList> arrayListKeyGroupList = new ArrayList<KeyGroupList>();
private ArrayList<KeyList> arrayListKeyList = new ArrayList<KeyList>();
private Business business;
private Datas datas;
private KeyGroupList keyGroupList;
private KeyList keyList;
/**
* 用Android自带的JSONObject和JSONArray方法
* @throws IOException
*/
public void getData1() throws IOException {
String strData = fileUtil.getFileData(this, "test.txt");
StringReader sr = new StringReader(strData);
JsonReader jr = new JsonReader(sr);
jr.beginObject();
business = new Business();
if (jr.nextName().contains("responseCode")) {
int responseCode = jr.nextInt();
Log.v("responseCode", "" + responseCode);
business.setResponseCode(responseCode);
}
if (jr.nextName().equals("msgInfo")) {
String msgInfo = jr.nextString();
Log.v("msgInfo", msgInfo);
business.setMsgInfo(msgInfo);
}
if (jr.nextName().equals("msgDetailInfo")) {
String msgDetailInfo = jr.nextString();
Log.v("msgDetailInfo", msgDetailInfo);
business.setMsgDetailInfo(msgDetailInfo);
}
if (jr.nextName().equals("data")) {
jr.beginArray();
while (jr.hasNext()) {
datas = new Datas();
jr.beginObject();
if (jr.nextName().equals("pbtId")) {
int pbtId = jr.nextInt();
Log.v("pbtId", "" + pbtId);
datas.setPbtId(pbtId);
}
if (jr.nextName().equals("pbtName")) {
String pbtName = jr.nextString();
Log.v("pbtName", "" + pbtName);
datas.setPbtName(pbtName);
}
if (jr.nextName().equals("keyGroupList")) {
jr.beginArray();
while (jr.hasNext()) {
keyGroupList = new KeyGroupList();
jr.beginObject();
if (jr.nextName().equals("keyGroupId")) {
int keyGroupId = jr.nextInt();
Log.v("keyGroupId", "" + keyGroupId);
keyGroupList.setKeyGroupId(keyGroupId);
}
if (jr.nextName().equals("itemId")) {
String itemId = jr.nextString();
Log.v("itemId", itemId);
keyGroupList.setItemId(itemId);
}
if (jr.nextName().equals("keyList")) {
jr.beginArray();
while (jr.hasNext()) {
keyList = new KeyList();
jr.beginObject();
if (jr.nextName().equals("keyValueId")) {
int keyValueId = jr.nextInt();
Log.v("keyValueId", "" + keyValueId);
keyList.setKeyValueId(keyValueId);
}
if (jr.nextName().equals("cityCode")) {
String cityCode = jr.nextString();
Log.v("cityCode", cityCode);
keyList.setCityCode(cityCode);
}
if (jr.nextName().equals("cityName")) {
String cityName = jr.nextString();
Log.v("cityName", cityName);
keyList.setCityName(cityName);
}
if (jr.nextName().equals("keyName")) {
String keyName = jr.nextString();
Log.v("keyName", keyName);
keyList.setKeyName(keyName);
}
if (jr.nextName().equals("keyText")) {
String keyText = jr.nextString();
Log.v("keyText", keyText);
keyList.setKeyText(keyText);
}
if (jr.nextName().equals("keyDesc")) {
String keyDesc = jr.nextString();
Log.v("keyDesc", keyDesc);
keyList.setKeyDesc(keyDesc);
}
if (jr.nextName().equals("keyValue")) {
String keyValue = jr.nextString();
Log.v("keyValue", keyValue);
keyList.setKeyValue(keyValue);
}
if (jr.nextName().equals("isPrivate")) {
int isPrivate = jr.nextInt();
Log.v("isPrivate", "" + isPrivate);
keyList.setIsPrivate(isPrivate);
}
jr.endObject();
arrayListKeyList.add(keyList);
}
jr.endArray();
keyGroupList.setArrayListKeyList(arrayListKeyList);
}
jr.endObject();
arrayListKeyGroupList.add(keyGroupList);
}
jr.endArray();
datas.setArrayListKeyGroupList(arrayListKeyGroupList);
}
jr.endObject();
arrayListDatas.add(datas);
}
jr.endArray();
business.setArrayListDatas(arrayListDatas);
}
jr.endObject();
arrayListBusiness.add(business);
}
解析的文档内容为:
{
"responseCode": 0,
"msgInfo": "业务处理成功。",
"msgDetailInfo": null,
"data": [
{
"pbtId": 1,
"pbtName": "公积金",
"keyGroupList": [
{
"keyGroupId": 1,
"itemId": 10001,
"keyList": [
{
"keyValueId": 1,
"cityCode": "3501",
"cityName": "福州",
"keyName": "housFundAccount",
"keyText": "账号",
"keyDesc": "公积金的账号",
"keyValue": "350104198001019999",
"isPrivate": "0"
},
{
"keyValueId": 2,
"cityCode": "3501",
"cityName": "福州",
"keyName": "housFundPassword",
"keyText": "密码",
"keyDesc": "公积金的密码",
"keyValue": "9999",
"isPrivate": "1"
}
]
},
{
"keyGroupId": 2,
"itemId": 10002,
"keyList": [
{
"keyValueId": 3,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "housFundAccount",
"keyText": "账号",
"keyDesc": "公积金的账号",
"keyValue": "350204198001018888",
"isPrivate": "0"
},
{
"keyValueId": 4,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "housFundPassword",
"keyText": "密码",
"keyDesc": "公积金的密码",
"keyValue": "8888",
"isPrivate": "1"
}
]
}
]
},
{
"pbtId": 2,
"pbtName": "医保",
"keyGroupList": [
{
"keyGroupId": 3,
"itemId": 10003,
"keyList": [
{
"keyValueId": 5,
"cityCode": "3501",
"cityName": "福州",
"keyName": "mediInsuranceAccount",
"keyText": "账号",
"keyDesc": "医保的账号",
"keyValue": "350104198001019999",
"isPrivate": "0"
},
{
"keyValueId": 6,
"cityCode": "3501",
"cityName": "福州",
"keyName": "mediInsurancePassword",
"keyText": "密码",
"keyDesc": "医保的密码",
"keyValue": "111111",
"isPrivate": "1"
}
]
},
{
"keyGroupId": 4,
"itemId": 10004,
"keyList": [
{
"keyValueId": 7,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "mediInsuranceAccount",
"keyText": "账号",
"keyDesc": "医保的账号",
"keyValue": "350204198001018888",
"isPrivate": "0"
},
{
"keyValueId": 8,
"cityCode": "3502",
"cityName": "厦门",
"keyName": "mediInsurancePassword",
"keyText": "密码",
"keyDesc": "医保的密码",
"keyValue": "000000",
"isPrivate": "1"
}
]
}
]
}
]
}
解析步骤:
第一步:创建不同的javaBean用于存储每一层的属性值;
第二步:若是JSON文档存放在Assets中,获取文档的内容存在在String中
/**
* 获取文件流
* @param context
* @return
* @throws IOException
*/
public InputStream getInputStream(Context context,String fileName) throws IOException{
AssetManager am=context.getAssets();
InputStream is=am.open(fileName);
return is;
}
/**
* 将文件中的内容,保存在String中
* @param is
* @return
* @throws IOException
*/
public String getFileData(InputStream is) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
String str;
while((str=br.readLine())!=null){
sb.append(str);
}
return sb.toString();
}
/**
* 获取文件资源内容
* @param context
* @param fileName
* @return
* @throws IOException
*/
public String getFileData(Context context,String fileName) throws IOException{
FileUtil fileUtil=new FileUtil();
InputStream is=fileUtil.getInputStream(context, fileName);
return fileUtil.getFileData(is);
}
第三步:解析文件
private ArrayList<Business> arrayListBusiness = new ArrayList<Business>();
private ArrayList<Datas> arrayListDatas = new ArrayList<Datas>();
private ArrayList<KeyGroupList> arrayListKeyGroupList = new ArrayList<KeyGroupList>();
private ArrayList<KeyList> arrayListKeyList = new ArrayList<KeyList>();
private Business business;
private Datas datas;
private KeyGroupList keyGroupList;
private KeyList keyList;
/**
* 用Android自带的JSONObject和JSONArray方法
* @throws IOException
*/
public void getData1() throws IOException {
String strData = fileUtil.getFileData(this, "test.txt");
StringReader sr = new StringReader(strData);
JsonReader jr = new JsonReader(sr);
jr.beginObject();
business = new Business();
if (jr.nextName().contains("responseCode")) {
int responseCode = jr.nextInt();
Log.v("responseCode", "" + responseCode);
business.setResponseCode(responseCode);
}
if (jr.nextName().equals("msgInfo")) {
String msgInfo = jr.nextString();
Log.v("msgInfo", msgInfo);
business.setMsgInfo(msgInfo);
}
if (jr.nextName().equals("msgDetailInfo")) {
String msgDetailInfo = jr.nextString();
Log.v("msgDetailInfo", msgDetailInfo);
business.setMsgDetailInfo(msgDetailInfo);
}
if (jr.nextName().equals("data")) {
jr.beginArray();
while (jr.hasNext()) {
datas = new Datas();
jr.beginObject();
if (jr.nextName().equals("pbtId")) {
int pbtId = jr.nextInt();
Log.v("pbtId", "" + pbtId);
datas.setPbtId(pbtId);
}
if (jr.nextName().equals("pbtName")) {
String pbtName = jr.nextString();
Log.v("pbtName", "" + pbtName);
datas.setPbtName(pbtName);
}
if (jr.nextName().equals("keyGroupList")) {
jr.beginArray();
while (jr.hasNext()) {
keyGroupList = new KeyGroupList();
jr.beginObject();
if (jr.nextName().equals("keyGroupId")) {
int keyGroupId = jr.nextInt();
Log.v("keyGroupId", "" + keyGroupId);
keyGroupList.setKeyGroupId(keyGroupId);
}
if (jr.nextName().equals("itemId")) {
String itemId = jr.nextString();
Log.v("itemId", itemId);
keyGroupList.setItemId(itemId);
}
if (jr.nextName().equals("keyList")) {
jr.beginArray();
while (jr.hasNext()) {
keyList = new KeyList();
jr.beginObject();
if (jr.nextName().equals("keyValueId")) {
int keyValueId = jr.nextInt();
Log.v("keyValueId", "" + keyValueId);
keyList.setKeyValueId(keyValueId);
}
if (jr.nextName().equals("cityCode")) {
String cityCode = jr.nextString();
Log.v("cityCode", cityCode);
keyList.setCityCode(cityCode);
}
if (jr.nextName().equals("cityName")) {
String cityName = jr.nextString();
Log.v("cityName", cityName);
keyList.setCityName(cityName);
}
if (jr.nextName().equals("keyName")) {
String keyName = jr.nextString();
Log.v("keyName", keyName);
keyList.setKeyName(keyName);
}
if (jr.nextName().equals("keyText")) {
String keyText = jr.nextString();
Log.v("keyText", keyText);
keyList.setKeyText(keyText);
}
if (jr.nextName().equals("keyDesc")) {
String keyDesc = jr.nextString();
Log.v("keyDesc", keyDesc);
keyList.setKeyDesc(keyDesc);
}
if (jr.nextName().equals("keyValue")) {
String keyValue = jr.nextString();
Log.v("keyValue", keyValue);
keyList.setKeyValue(keyValue);
}
if (jr.nextName().equals("isPrivate")) {
int isPrivate = jr.nextInt();
Log.v("isPrivate", "" + isPrivate);
keyList.setIsPrivate(isPrivate);
}
jr.endObject();
arrayListKeyList.add(keyList);
}
jr.endArray();
keyGroupList.setArrayListKeyList(arrayListKeyList);
}
jr.endObject();
arrayListKeyGroupList.add(keyGroupList);
}
jr.endArray();
datas.setArrayListKeyGroupList(arrayListKeyGroupList);
}
jr.endObject();
arrayListDatas.add(datas);
}
jr.endArray();
business.setArrayListDatas(arrayListDatas);
}
jr.endObject();
arrayListBusiness.add(business);
}
相关文章推荐
- 用Google提供的Gson解析JSON数据
- Gson全解析之二:JsonReader的其它方法
- Android利用Gson解析嵌套多层的Json的简单方法
- .Net利用Newtonsoft进行解析Json的快捷方法
- Gson解析Json的方法 泛型解决方案
- Android 编程下使用 Google 的 Gson 解析 Json
- TypeToken 是google提供的一个解析Json数据的类库中一个类
- Amdroid示例:利用Gson生成或解析json
- struts2在idea中加入json的jar包后,引用无法解析的解决方法
- 利用Gson解析复杂json
- Android中使用Gson解析JSON数据的两种方法
- Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
- 为Silverlight 提供将Json解析为Geometry的方法
- 使用Gson解析json在打包混淆时出错的解决方法
- 很好用的Google 开源项目Gson在java中解析和创建json格式的数据
- java.lang.NoClassDefFoundError: com.google.gson.stream.JsonReader 类似的在eclipse通过bulidpath导入包发生异常
- 很好用的Google 开源项目Gson在java中解析和创建json格式的数据
- 用Gson解析纯json数组的方法总结
- 利用Gson将对象转换成json,忽略某些字段的方法
- 利用GSON解析简单Json字符串