2019-04-05 23:59:31 +08:00
|
|
|
package com.novelbook.android.utils;
|
|
|
|
|
2019-04-12 23:02:31 +08:00
|
|
|
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.NovelBlock;
|
2019-04-05 23:59:31 +08:00
|
|
|
import com.novelbook.android.db.Novel;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2019-04-12 23:02:31 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-04-05 23:59:31 +08:00
|
|
|
public class GsonUtil {
|
|
|
|
|
2019-04-12 23:02:31 +08:00
|
|
|
private static Gson gson = new Gson();
|
2019-04-05 23:59:31 +08:00
|
|
|
public static Novel getNovel(String json){
|
|
|
|
Novel nv = new Novel();
|
|
|
|
try {
|
|
|
|
JSONObject jsonObject = new JSONObject(json);
|
2019-04-09 22:32:02 +08:00
|
|
|
nv.setNovelId(jsonObject.getInt("novelId"));
|
2019-04-05 23:59:31 +08:00
|
|
|
nv.setLastUpateTime(jsonObject.getLong("lastUpateTime"));
|
|
|
|
nv.setAuthor(jsonObject.getString("author"));
|
|
|
|
nv.setName(jsonObject.getString("name"));
|
|
|
|
nv.setCover(jsonObject.getString("cover"));
|
|
|
|
nv.setNovelType(jsonObject.getString("novelType"));
|
2019-04-09 22:32:02 +08:00
|
|
|
nv.setSmallNovelType(jsonObject.getString("novelType2"));
|
|
|
|
nv.setChapterName(jsonObject.getString("lastestChapterName"));
|
2019-04-05 23:59:31 +08:00
|
|
|
|
|
|
|
return nv;
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return nv;
|
|
|
|
}
|
2019-04-12 23:02:31 +08:00
|
|
|
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对象
|
|
|
|
list.add((NovelBlock) gson.fromJson(e, NovelBlock.class));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
public static List<Novel> parserJsonArray(String restult,String blockName ) throws JSONException {
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(restult);
|
2019-04-16 23:11:00 +08:00
|
|
|
return parserJsonArray(jsonObject,blockName);
|
|
|
|
}
|
|
|
|
public static List<Novel> parserJsonArray( JSONObject jsonObject,String blockName ) throws JSONException {
|
|
|
|
|
2019-04-12 23:02:31 +08:00
|
|
|
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();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
JsonElement e = (JsonElement) it.next();
|
|
|
|
//JsonElement转换为JavaBean对象
|
|
|
|
list.add((Novel) gson.fromJson(e, Novel.class));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2019-04-05 23:59:31 +08:00
|
|
|
}
|