use of com.google.gson.JsonElement in project Avengers by saulmm.
the class GsonDeserializersTest method testThatACollectionDeserialzierDeserializesACollection.
@Test
public void testThatACollectionDeserialzierDeserializesACollection() throws Exception {
MarvelResultsDeserializer<CollectionItem> marvelResultsDeserializer = new MarvelResultsDeserializer<>();
JsonElement collectionElement = new JsonParser().parse(getComicsCollectionJsonString());
Type t = new TypeToken<List<CollectionItem>>() {
}.getType();
List<CollectionItem> collectionList = marvelResultsDeserializer.deserialize(collectionElement, t, mock(JsonDeserializationContext.class));
assertThat(collectionList.isEmpty(), is(false));
assertNotNull(collectionList.get(0).getThumbnail());
}
use of com.google.gson.JsonElement in project play-cookbook by spinscale.
the class ApiPlugin method getJson.
private Object getJson(Class clazz, String name) {
try {
String data = IOUtils.toString(Request.current().body);
JsonElement jsonElem = new JsonParser().parse(data);
if (jsonElem.isJsonObject()) {
JsonObject json = (JsonObject) jsonElem;
if (json.has(name)) {
JsonObject from = json.getAsJsonObject(name);
Object result = gson.fromJson(from, clazz);
return result;
}
}
} catch (Exception e) {
Logger.error("Problem rendering JSON: %s", e.getMessage());
}
return null;
}
use of com.google.gson.JsonElement in project play-cookbook by spinscale.
the class ApiPlugin method getJson.
private Object getJson(Class clazz, String name) {
try {
String body = Request.current().params.get("body");
JsonElement jsonElem = new JsonParser().parse(body);
if (jsonElem.isJsonObject()) {
JsonObject json = (JsonObject) jsonElem;
if (json.has(name)) {
JsonObject from = json.getAsJsonObject(name);
Object result = gson.fromJson(from, clazz);
return result;
}
}
} catch (Exception e) {
Logger.error("Problem rendering JSON: %s", e.getMessage());
}
return null;
}
use of com.google.gson.JsonElement in project SimpleNews by liuling07.
the class NewsJsonUtils method readJsonNewsDetailBeans.
public static NewsDetailBean readJsonNewsDetailBeans(String res, String docId) {
NewsDetailBean newsDetailBean = null;
try {
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(res).getAsJsonObject();
JsonElement jsonElement = jsonObj.get(docId);
if (jsonElement == null) {
return null;
}
newsDetailBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), NewsDetailBean.class);
} catch (Exception e) {
LogUtils.e(TAG, "readJsonNewsBeans error", e);
}
return newsDetailBean;
}
use of com.google.gson.JsonElement in project SimpleNews by liuling07.
the class WeatherJsonUtils method getCity.
/**
* 从定位的json字串中获取城市
* @param json
* @return
*/
public static String getCity(String json) {
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(json).getAsJsonObject();
JsonElement status = jsonObj.get("status");
if (status != null && "OK".equals(status.getAsString())) {
JsonObject result = jsonObj.getAsJsonObject("result");
if (result != null) {
JsonObject addressComponent = result.getAsJsonObject("addressComponent");
if (addressComponent != null) {
JsonElement cityElement = addressComponent.get("city");
if (cityElement != null) {
return cityElement.getAsString().replace("市", "");
}
}
}
}
return null;
}
Aggregations