What is different between JsonObject and JSONObject?
I am little bit confuse JsonObject and JSONObject and when we use put, add and addproperty method.
thanks in advance.
This isnt base Java, those are Objects depending on the JSON-libary you are using. JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one.
JSON is a string format. The data is only JSON when it is in a string format. When it is converted to a JavaScript variable, it becomes a JavaScript object.
JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values.
JsonObject class represents an immutable JSON object value (an unordered collection of zero or more name/value pairs). It also provides unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using JsonReader. readObject() .
The Android SDK provides JSONObject
. This can be used by importing org.json.JSONObject
.
The documentation for JSONObject is here.
Any other variant will be from a library or linked project. The exmaple given by cricket_007 is JsonObject
from the Gson libraries. This can be used by importing com.google.gson.JsonObject
.
The documentation for JsonObject is here.
Note the difference in import statement for JSONObject
and JsonObject
- they are different classes in different packages. They can have different methods and functionality, take different parameters etc, but ultimately they will do the same or similar thing (holding a Json Object's contents). It is up to you which you use. The JavaDoc for each will describe how to use it, and there are plenty of resources online for each.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With