Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JSONObject and JSONArray

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?

like image 785
Luke Taylor Avatar asked Sep 05 '12 21:09

Luke Taylor


People also ask

What is the difference between JSON object and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

What is the use of JSONArray?

JsonArray provides various accessor methods to access the values in an array. The following example shows how to obtain the home phone number "212 555-1234" from the array built in the previous example: JsonObject home = array. getJsonObject(0); String number = home.

What is the difference between JSON object and JSON object in Android?

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. I had some issue with the date formatting in JSONObject.

What is difference between JSON string and JSON object?

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.


2 Answers

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:

{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]} 
like image 165
Eric Levine Avatar answered Oct 13 '22 09:10

Eric Levine


The difference is the same as a (Hash)Map vs List.

JSONObject:

  • Contains named values (key->value pairs, tuples or whatever you want to call them)
    • like {ID : 1}
  • Order of elements is not important
    • a JSONObject of {id: 1, name: 'B'} is equal to {name: 'B', id: 1}.

JSONArray:

  • Contains only series values
    • like [1, 'value']
  • Order of values is important
    • array of [1,'value'] is not the same as ['value',1]

Example

JSON Object --> { "":""}  JSON Array --> [ , , , ]  {"employees":[     {"firstName":"John", "lastName":"Doe"},     {"firstName":"Anna", "lastName":"Smith"},     {"firstName":"Peter", "lastName":"Jones"} ]} 
like image 41
Aleksandr Panzin Avatar answered Oct 13 '22 07:10

Aleksandr Panzin