208 lines
7.3 KiB
Plaintext
208 lines
7.3 KiB
Plaintext
|
package com.novelbook.android.utils;
|
||
|
|
||
|
import com.google.gson.Gson;
|
||
|
import com.google.gson.JsonArray;
|
||
|
import com.google.gson.JsonElement;
|
||
|
import com.google.gson.JsonObject;
|
||
|
import com.google.gson.JsonParser;
|
||
|
import com.novelbook.android.bean.BangdanCate;
|
||
|
import com.novelbook.android.bean.NovelBlock;
|
||
|
import com.novelbook.android.bean.ProgressType;
|
||
|
import com.novelbook.android.bean.ProgressType;
|
||
|
import com.novelbook.android.db.Novel;
|
||
|
|
||
|
import org.json.JSONArray;
|
||
|
import org.json.JSONException;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class GsonUtil {
|
||
|
|
||
|
private static Gson gson = new Gson();
|
||
|
|
||
|
public static List<String>
|
||
|
/*public static Novel getNovel(String json){
|
||
|
Novel nv = new Novel();
|
||
|
try {
|
||
|
JSONObject jsonObject = new JSONObject(json);
|
||
|
nv.setNovelId(jsonObject.getInt("novelId"));
|
||
|
nv.setLastUpdateTime(jsonObject.getLong("lastUpateTime"));
|
||
|
nv.setAuthor(jsonObject.getString("author"));
|
||
|
nv.setName(jsonObject.getString("name"));
|
||
|
nv.setCover(jsonObject.getString("cover"));
|
||
|
nv.setNovelType(jsonObject.getString("novelType"));
|
||
|
nv.setSmallNovelType(jsonObject.getString("novelType2"));
|
||
|
nv.setChapterName(jsonObject.getString("lastestChapterName"));
|
||
|
|
||
|
return nv;
|
||
|
|
||
|
} catch (JSONException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return nv;
|
||
|
}*/parserStringBlocks(String result,String blockName ) throws JSONException {
|
||
|
JSONObject jsonObject = new JSONObject(result);
|
||
|
JSONArray array = jsonObject.getJSONArray(blockName);
|
||
|
List<String> lst = new ArrayList<String>();
|
||
|
for( int i=0;i< array.length();i++){
|
||
|
lst .add(array.getString(i));
|
||
|
}
|
||
|
return lst;
|
||
|
}
|
||
|
public static List<ProgressType> parserProgressType(String restult, String blockName ) throws JSONException {
|
||
|
JSONObject jsonObject = new JSONObject(restult);
|
||
|
String strJson = jsonObject.getString(blockName);
|
||
|
List<ProgressType> list = new ArrayList<ProgressType>();
|
||
|
//创建一个Gson对象
|
||
|
// Gson gson = new Gson();
|
||
|
//创建一个JsonParser
|
||
|
JsonParser parser = new JsonParser();
|
||
|
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
|
||
|
JsonElement el = parser.parse(strJson);
|
||
|
|
||
|
//把JsonElement对象转换成JsonObject
|
||
|
JsonObject jsonObj = null;
|
||
|
if (el.isJsonObject()) {
|
||
|
jsonObj = el.getAsJsonObject();
|
||
|
}
|
||
|
|
||
|
|
||
|
//把JsonElement对象转换成JsonArray
|
||
|
JsonArray jsonArray = null;
|
||
|
if (el.isJsonArray()) {
|
||
|
jsonArray = el.getAsJsonArray();
|
||
|
}
|
||
|
|
||
|
//遍历JsonArray对象
|
||
|
Iterator it = jsonArray.iterator();
|
||
|
while (it.hasNext()) {
|
||
|
JsonElement e = (JsonElement) it.next();
|
||
|
//JsonElement转换为JavaBean对象
|
||
|
list.add((ProgressType) gson.fromJson(e, ProgressType.class));
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
public static List<BangdanCate> parserBangdanCate(String restult, String blockName ) throws JSONException {
|
||
|
JSONObject jsonObject = new JSONObject(restult);
|
||
|
String strJson = jsonObject.getString(blockName);
|
||
|
List<BangdanCate> list = new ArrayList<BangdanCate>();
|
||
|
//创建一个Gson对象
|
||
|
// Gson gson = new Gson();
|
||
|
//创建一个JsonParser
|
||
|
JsonParser parser = new JsonParser();
|
||
|
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
|
||
|
JsonElement el = parser.parse(strJson);
|
||
|
|
||
|
//把JsonElement对象转换成JsonObject
|
||
|
JsonObject jsonObj = null;
|
||
|
if (el.isJsonObject()) {
|
||
|
jsonObj = el.getAsJsonObject();
|
||
|
}
|
||
|
|
||
|
|
||
|
//把JsonElement对象转换成JsonArray
|
||
|
JsonArray jsonArray = null;
|
||
|
if (el.isJsonArray()) {
|
||
|
jsonArray = el.getAsJsonArray();
|
||
|
}
|
||
|
|
||
|
//遍历JsonArray对象
|
||
|
Iterator it = jsonArray.iterator();
|
||
|
while (it.hasNext()) {
|
||
|
JsonElement e = (JsonElement) it.next();
|
||
|
//JsonElement转换为JavaBean对象
|
||
|
list.add((BangdanCate) gson.fromJson(e, BangdanCate.class));
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
|
public static List<NovelBlock> parserNovleBlocks(String restult,String blockName ) throws JSONException {
|
||
|
JSONObject jsonObject = new JSONObject(restult);
|
||
|
String strJson = jsonObject.getString(blockName);
|
||
|
List<NovelBlock> list = new ArrayList<NovelBlock>();
|
||
|
//创建一个Gson对象
|
||
|
// Gson gson = new Gson();
|
||
|
//创建一个JsonParser
|
||
|
JsonParser parser = new JsonParser();
|
||
|
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
|
||
|
JsonElement el = parser.parse(strJson);
|
||
|
|
||
|
//把JsonElement对象转换成JsonObject
|
||
|
JsonObject jsonObj = null;
|
||
|
if (el.isJsonObject()) {
|
||
|
jsonObj = el.getAsJsonObject();
|
||
|
}
|
||
|
|
||
|
|
||
|
//把JsonElement对象转换成JsonArray
|
||
|
JsonArray jsonArray = null;
|
||
|
if (el.isJsonArray()) {
|
||
|
jsonArray = el.getAsJsonArray();
|
||
|
}
|
||
|
|
||
|
//遍历JsonArray对象
|
||
|
Iterator it = jsonArray.iterator();
|
||
|
while (it.hasNext()) {
|
||
|
JsonElement e = (JsonElement) it.next();
|
||
|
//JsonElement转换为JavaBean对象
|
||
|
NovelBlock nb = (NovelBlock) gson.fromJson(e, NovelBlock.class);
|
||
|
if(nb.getNs().size()>0) {
|
||
|
list.add(nb);
|
||
|
}
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
public static List<Novel> parserJsonArray(String restult,String blockName ) throws JSONException {
|
||
|
|
||
|
JSONObject jsonObject = new JSONObject(restult);
|
||
|
return parserJsonArray(jsonObject,blockName);
|
||
|
}
|
||
|
public static List<Novel> parserJsonArray( JSONObject jsonObject,String blockName ) throws JSONException {
|
||
|
|
||
|
String resultstr = jsonObject.getString(blockName);
|
||
|
|
||
|
List<Novel> list = new ArrayList<Novel>();
|
||
|
//创建一个Gson对象
|
||
|
// Gson gson = new Gson();
|
||
|
//创建一个JsonParser
|
||
|
JsonParser parser = new JsonParser();
|
||
|
//通过JsonParser对象可以把json格式的字符串解析成一个JsonElement对象
|
||
|
JsonElement el = parser.parse(resultstr);
|
||
|
|
||
|
//把JsonElement对象转换成JsonObject
|
||
|
JsonObject jsonObj = null;
|
||
|
if (el.isJsonObject()) {
|
||
|
jsonObj = el.getAsJsonObject();
|
||
|
}
|
||
|
|
||
|
|
||
|
//把JsonElement对象转换成JsonArray
|
||
|
JsonArray jsonArray = null;
|
||
|
if (el.isJsonArray()) {
|
||
|
jsonArray = el.getAsJsonArray();
|
||
|
}
|
||
|
|
||
|
//遍历JsonArray对象
|
||
|
Iterator it = jsonArray.iterator();
|
||
|
// int lastNoveId =0;
|
||
|
List<Integer> ids = new ArrayList<Integer>();
|
||
|
while (it.hasNext()) {
|
||
|
JsonElement e = (JsonElement) it.next();
|
||
|
//JsonElement转换为JavaBean对象
|
||
|
Novel nv = (Novel) gson.fromJson(e, Novel.class);
|
||
|
if(ids.contains(nv.getNovelId())){
|
||
|
continue;
|
||
|
}
|
||
|
ids.add(nv.getNovelId());
|
||
|
// lastNoveId =nv.getNovelId();
|
||
|
if(nv!=null) {
|
||
|
list.add((Novel) gson.fromJson(e, Novel.class));
|
||
|
}
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
}
|