205 lines
7.2 KiB
Plaintext
205 lines
7.2 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();
|
|
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 (nv == null || ids.contains(nv.getNovelId())) {
|
|
continue;
|
|
}
|
|
ids.add(nv.getNovelId());
|
|
list.add((Novel) gson.fromJson(e, Novel.class));
|
|
|
|
}
|
|
return list;
|
|
}
|
|
}
|