After having a short look at Google I found this link that describes the difference, yet from a syntax point of view.
When would one be preferred over the other in a programming scenario?
After having a short look at Google I found this link that describes the difference, yet from a syntax point of view.
When would one be preferred over the other in a programming scenario?
object : An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
array : An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
When you are working with JSON data in Android, you would use
JSONArray
to parse JSON which starts with the array brackets. Arrays in JSON are used to organize a collection of related items (Which could be JSON objects).For example:
[{"name":"item 1"},{"name": "item2} ]
On the other hand, you would use
JSONObject
when dealing with JSON that begins with curly braces. A JSON object is typically used to contain key/value pairs related to one item. For example:{"name": "item1", "description":"a JSON object"}
Of course, JSON arrays and objects may be nested inside one another. One common example of this is an API which returns a JSON object containing some metadata alongside an array of the items matching your query:
I know, all of the previous answers are insightful to your question. I had too like you this confusion just one minute before finding this SO thread. After reading some of the answers, here is what I get: A JSONObject is a JSON-like object that can be represented as an element in the array, the JSONArray. In other words, a JSONArray can contain a (or many) JSONObject.
I always use object, it is more easily extendable, JSON array is not. For example you originally had some data as a json array, then you needed to add a status header on it you'd be a bit stuck, unless you'd nested the data in an object. The only disadvantage is a slight increase in complexity of creation / parsing.
So instead of
You'd have
then later you can add more...
To understand it in a easier way, following are the diffrences between JSON object and JSON array:
Link to Tabular Difference : https://i.stack.imgur.com/GIqI9.png
JSON Array
JSON Object
Best programmatically Understanding.
A
JSONObject
is a JSON-like object that can be represented as an element in theJSONArray
.JSONArray
can contain a (or many)JSONObject
Hope this will helpful to you !