本文整理汇总了Java中com.google.gson.JsonElement.getAsInt方法的典型用法代码示例。如果您正苦于以下问题:Java JsonElement.getAsInt方法的具体用法?Java JsonElement.getAsInt怎么用?Java JsonElement.getAsInt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.JsonElement
的用法示例。
在下文中一共展示了JsonElement.getAsInt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Error
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public Error(JsonObject fields) {
JsonElement message = fields.get("message");
if (message != null && message.isJsonPrimitive() && message.getAsJsonPrimitive().isString()) {
this.message = message.getAsString();
} else {
this.message = "Unknown error";
}
JsonElement line = fields.get("line");
if (line != null && line.isJsonPrimitive() && line.getAsJsonPrimitive().isNumber()) {
this.line = line.getAsInt();
} else {
this.line = 0;
}
JsonElement column = fields.get("column");
if (column != null && column.isJsonPrimitive() && column.getAsJsonPrimitive().isNumber()) {
this.column = column.getAsInt();
} else {
this.column = 0;
}
}
示例2: load
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public void load(Match match) {
if (match.getMapContainer().getMapInfo().getJsonObject().has("points")) {
JsonObject pointsJson = match.getMapContainer().getMapInfo().getJsonObject().get("points").getAsJsonObject();
JsonElement targetElement = pointsJson.get("target");
if (targetElement.isJsonPrimitive()) {
int target = targetElement.getAsInt();
for (MatchTeam matchTeam : TGM.get().getModule(TeamManagerModule.class).getTeams()) {
if (!matchTeam.isSpectator()) {
targets.put(matchTeam, target);
}
}
} else {
//todo: per team target parsing
}
}
}
示例3: testGetAllDocs
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Test
public void testGetAllDocs() {
createCollection();
insertDocInDB();
insertDocInDB();
RestHeartClientResponse response = api.getAllDocumentsFromCollection(dbName, collName);
Assert.assertNotNull("Response is null", response);
JsonObject responseObject = response.getResponseObject();
Assert.assertNotNull("Json object response is null", responseObject);
JsonElement returned = responseObject.get("_returned");
int numberOfElements = returned.getAsInt();
Assert.assertEquals("response size not as expected", 2, numberOfElements);
LOGGER.info(GsonUtils.toJson(response.getResponseObject()));
}
示例4: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static int getInt(JsonElement p_151215_0_, String p_151215_1_)
{
if (p_151215_0_.isJsonPrimitive() && p_151215_0_.getAsJsonPrimitive().isNumber())
{
return p_151215_0_.getAsInt();
}
else
{
throw new JsonSyntaxException("Expected " + p_151215_1_ + " to be a Int, was " + toString(p_151215_0_));
}
}
示例5: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
* Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's
* field if an error message needs to be thrown.
*/
public static int getInt(JsonElement json, String memberName)
{
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber())
{
return json.getAsInt();
}
else
{
throw new JsonSyntaxException("Expected " + memberName + " to be a Int, was " + toString(json));
}
}
示例6: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public CustomCommentsResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = json.getAsJsonObject();
CustomCommentsResponse response = new CustomCommentsResponse();
if (root.has("main")) {
JsonElement main = root.get("main");
if (!main.isJsonPrimitive()) {
response.main = context.deserialize(main, CustomCommentsResponse.Main.class);
} // "main": false (if has execute errors)
}
if (root.has("first_id")) {
JsonElement firstIdJson = root.get("first_id");
response.firstId = firstIdJson.isJsonNull() ? null : firstIdJson.getAsInt();
}
if (root.has("last_id")) {
JsonElement lastIdJson = root.get("last_id");
response.lastId = lastIdJson.isJsonNull() ? null : lastIdJson.getAsInt();
}
if (root.has("admin_level")) {
JsonElement adminLevelJson = root.get("admin_level");
response.admin_level = adminLevelJson.isJsonNull() ? null : adminLevelJson.getAsInt();
}
return response;
}
示例7: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
protected int getInt(String field, int failover) {
if (!obj.has(field)) {
return failover;
}
JsonElement el = obj.get(field);
if (el.isJsonNull()) {
return failover;
}
return el.getAsInt();
}
示例8: extractInteger
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public int extractInteger(JsonObject json, String name, int defaultValue) {
if (json != null) {
int dotIndex = name.indexOf('.');
if (dotIndex > 0) {
String baseName = name.substring(0, dotIndex);
JsonElement childElement = json.get(baseName);
return extractInteger((JsonObject) childElement, name.substring(dotIndex + 1), defaultValue);
}
JsonElement element = json.get(name);
if (element != null && ! element.isJsonNull()) {
return element.getAsInt();
}
}
return defaultValue;
}
示例9: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return json.getAsInt();
} catch (Exception e) {
TLog.log("IntegerJsonDeserializer-deserialize-error:" + (json != null ? json.toString() : ""));
return 0;
}
}
示例10: getInteger
import com.google.gson.JsonElement; //导入方法依赖的package包/类
/**
*
* @param path
* @return
*/
public Integer getInteger(String path, Integer defaultVal) {
JsonElement jsonElt = getLastJsonEltInPath(path);
if (jsonElt == null || jsonElt.isJsonNull()) {
return defaultVal;
} else {
return jsonElt.getAsInt();
}
}
示例11: getValue
import com.google.gson.JsonElement; //导入方法依赖的package包/类
private static <T> T getValue(JsonElement element, T compared) throws IllegalStateException, ClassCastException, IllegalArgumentException {
if (compared instanceof Double) {
return (T)(new Double(element.getAsDouble()));
}
else if (compared instanceof Integer) {
return (T)(new Integer(element.getAsInt()));
}
else if (compared instanceof JsonObject) {
return (T)element.getAsJsonObject();
}
else if (compared instanceof JsonArray) {
return (T)element.getAsJsonArray();
}
else if (compared instanceof String) {
return (T)element.getAsString();
}
else if (compared instanceof Boolean) {
return (T)(new Boolean(element.getAsBoolean()));
}
throw new IllegalArgumentException();
}
示例12: getInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
public int getInt(String elementName) {
JsonElement element = jsonObject.get(elementName);
return element.getAsInt();
}
示例13: jsonMemberToInt
import com.google.gson.JsonElement; //导入方法依赖的package包/类
private static int jsonMemberToInt(JsonElement jsonElement, String memberName) {
JsonElement jsonMemberElement = jsonElement.getAsJsonObject().get(memberName);
return jsonMemberElement != null ? jsonMemberElement.getAsInt() : 0;
}
示例14: deserialize
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public VKApiVideo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject root = json.getAsJsonObject();
VKApiVideo dto = new VKApiVideo();
dto.id = optInt(root, "id");
dto.owner_id = optInt(root, "owner_id");
dto.title = optString(root, "title");
dto.description = optString(root, "description");
dto.duration = optInt(root, "duration");
dto.link = optString(root, "link");
dto.date = optLong(root, "date");
dto.adding_date = optLong(root, "adding_date");
dto.views = optInt(root, "views");
JsonElement commentJson = root.get("comments");
if(nonNull(commentJson)){
if(commentJson.isJsonObject()){
//for example, newsfeed.getComment
dto.comments = context.deserialize(commentJson, CommentsDto.class);
} else {
// video.get
dto.comments = new CommentsDto();
dto.comments.count = commentJson.getAsInt();
}
}
dto.player = optString(root, "player");
dto.access_key = optString(root, "access_key");
dto.album_id = optInt(root, "album_id");
if(root.has("likes")){
JsonObject likesRoot = root.getAsJsonObject("likes");
dto.likes = optInt(likesRoot, "count");
dto.user_likes = optIntAsBoolean(likesRoot, "user_likes");
}
dto.can_comment = optIntAsBoolean(root, "can_comment");
dto.can_repost = optIntAsBoolean(root, "can_repost");
dto.repeat = optIntAsBoolean(root, "repeat");
if(root.has("privacy_view")){
dto.privacy_view = context.deserialize(root.get("privacy_view"), VkApiPrivacy.class);
}
if(root.has("privacy_comment")){
dto.privacy_comment = context.deserialize(root.get("privacy_comment"), VkApiPrivacy.class);
}
if(root.has("files")){
JsonObject filesRoot = root.getAsJsonObject("files");
dto.mp4_240 = optString(filesRoot, "mp4_240");
dto.mp4_360 = optString(filesRoot, "mp4_360");
dto.mp4_480 = optString(filesRoot, "mp4_480");
dto.mp4_720 = optString(filesRoot, "mp4_720");
dto.mp4_1080 = optString(filesRoot, "mp4_1080");
dto.external = optString(filesRoot, "external");
}
dto.photo_130 = optString(root, "photo_130");
dto.photo_320 = optString(root, "photo_320");
dto.photo_800 = optString(root, "photo_800");
dto.platform = optString(root, "platform");
dto.can_edit = optIntAsBoolean(root, "can_edit");
dto.can_add = optIntAsBoolean(root, "can_add");
return dto;
}
示例15: fromJson
import com.google.gson.JsonElement; //导入方法依赖的package包/类
@Override
public Integer fromJson(final JsonElement json)
{
return json.getAsInt();
}