Google's Json Parsing Gson library: What's the difference between JsonElement and JsonObject?

前端 未结 2 604
死守一世寂寞
死守一世寂寞 2021-02-03 19:23
public abstract class JsonElement extends Object 

A class representing an element of Json. It could either be a JsonObject, a JsonArray, a JsonPrimitiv

相关标签:
2条回答
  • 2021-02-03 19:54

    JsonElement contains common code for all the valid types in JSON:

    • JsonObject
    • JsonArray
    • JsonPrimitive (string, number, boolean)
    • JsonNull

    This allows you a write a method that takes a JsonElement that works with any of the above types.

    0 讨论(0)
  • 2021-02-03 20:07

    JsonElement is the base type for all of the different specific types - it's a base class with common things that all nodes should implement. Logically, this makes a lot of sense.

    The thing that all specific types inherits from JsonElement is a good thing for you, too. It allows you to test if an object is a Json node representation, simply by checking if it inherits from JsonElement.

    The JsonElement class is available to you as a public class probably not because you'd want to subclass it, but because you'd want to test for its subclasses.

    0 讨论(0)
提交回复
热议问题