GSON JsonElement.getAsString与JsonElement.toString吗? [英] GSON JsonElement.getAsString vs. JsonElement.toString?

查看:411
本文介绍了GSON JsonElement.getAsString与JsonElement.toString吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JsonElement#getAsString()JsonElement#toString()有什么区别?

在某些情况下应该使用另一种吗?

Are there situations where one should be used over the other?

推荐答案

假设您是指

Assuming that you are referring to JsonElement:

getAsString()

便捷方法,用于获取此元素作为字符串值.

convenience method to get this element as a string value.

此方法访问并返回元素的属性,即,元素的值作为java String对象.

This method accesses and returns a property of the element, i.e. the value of the element as a java String object.

toString()

返回此元素的字符串表示形式.

Returns a String representation of this element.

此方法是标准" java

This method is the "standard" java toString method, i.e. returns a human readable representation of the element itself.

为了更好地理解,让我给您举个例子:

For better understanding, let me give you an example:

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

public class GsonTest {

    public static void main(String[] args) {
        JsonElement jsonElement = new JsonPrimitive("foo");

        System.out.println(jsonElement.toString());
        System.out.println(jsonElement.getAsString());

        jsonElement = new JsonPrimitive(42);

        System.out.println(jsonElement.toString());
        System.out.println(jsonElement.getAsString());

        jsonElement = new JsonPrimitive(true);

        System.out.println(jsonElement.toString());
        System.out.println(jsonElement.getAsString());

        jsonElement = new JsonObject();
        ((JsonObject) jsonElement).addProperty("foo", "bar");
        ((JsonObject) jsonElement).addProperty("foo2", 42);

        System.out.println(jsonElement.toString());
        System.out.println(jsonElement.getAsString());
    }
}

输出:

"foo"
foo
42
42
true
true
{"foo":"bar","foo2":42}
Exception in thread "main" java.lang.UnsupportedOperationException: JsonObject
    at com.google.gson.JsonElement.getAsString(JsonElement.java:185)

如您所见,输出在某些情况下非常相似(甚至相等),但在其他情况下则有所不同. getAsString()仅为基本类型(和仅包含单个基本元素的数组)定义,并且在调用例如时抛出异常.在一个物体上. toString()将适用于所有类型的JsonElement.

As you can see, the output is in some cases quite similar (or even equals), but it differs in some other cases. getAsString() is only defined for primitive types (and arrays containing only a single primitive element) and throws an exception if called e.g. on an object. toString() will work on all types of JsonElement.

现在应该何时使用哪种方法?

Now when should you use which method?

  • 如果要打印出调试信息,请使用toString()
  • 如果您知道要处理的是原始类型,并且想要在某个地方显示或写入值,请使用getAsString()
  • 如果您不知道类型,或者想使用该值工作(即进行计算),请不要使用两者.而是检查元素的类型并使用适当的方法.
  • If you want to print out debug information, use toString()
  • If you know that you are dealing with a primitive type and you want to display or write the value somewhere, use getAsString()
  • If you don't know the type or if you want to work with the value (i.e. do calculations), use neither. Instead check for the type of the element and use the appropriate method.

这篇关于GSON JsonElement.getAsString与JsonElement.toString吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆