Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

gson

public abstract class JsonElement extends Object  

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

public final class JsonObject extends JsonElement  

A class representing an object type in Json. An object consists of name-value pairs where names are strings, and values are any other type of JsonElement. This allows for a creating a tree of JsonElements. The member elements of this object are maintained in order they were added.

Yay google!

Nevermind that question.

like image 997
Teddy Avatar asked Jun 08 '12 19:06

Teddy


People also ask

What is Jsonobject and JsonElement?

A class representing an object type in Json. An object consists of name-value pairs where names are strings, and values are any other type of JsonElement . This allows for a creating a tree of JsonElements. The member elements of this object are maintained in order they were added.

Is GSON better than Json?

Looking at the average result for all the test runs across all the files, GSON is the winner here, with JSON.

What is a JsonPrimitive?

public final class JsonPrimitive extends JsonElement. A class representing a Json primitive value. A primitive value is either a String, a Java primitive, or a Java primitive wrapper type.


1 Answers

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.

like image 84
Juan Mendes Avatar answered Sep 27 '22 20:09

Juan Mendes