Answers:
在Android中使用JSON数据时,您将使用JSONArray
解析以数组括号开头的JSON。JSON中的数组用于组织相关项的集合(可能是JSON对象)。
例如:[{"name":"item 1"},{"name": "item2} ]
另一方面,JSONObject
在处理以花括号开头的JSON时,您会使用。JSON对象通常用于包含与一项相关的键/值对。例如:{"name": "item1", "description":"a JSON object"}
当然,JSON数组和对象可以相互嵌套。一个常见的示例是一个API,该API返回一个JSON对象,该对象包含一些元数据以及与您的查询匹配的项目数组:
{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]}
区别与(Hash)Map vs List相同。
JSONObject:
{ID : 1}
{id: 1, name: 'B'}
等于{name: 'B', id: 1}
。JSONArray:
[1, 'value']
[1,'value']
与['value',1]
例
JSON Object --> { "":""}
JSON Array --> [ , , , ]
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
最好以编程方式理解。
当语法是
{}
那么JsonObject
当语法是
[]
那么JsonArray
A JSONObject
是一个类似于JSON的对象,可以表示为中的元素JSONArray
。JSONArray
可以包含一个(或多个)JSONObject
希望这对您有所帮助!
为了更轻松地理解它,以下是JSON对象和JSON数组之间的区别:
链接到表格差异:https : //i.stack.imgur.com/GIqI9.png
JSON数组
1. Arrays in JSON are used to organize a collection of related items
(Which could be JSON objects)
2. Array values must be of type string, number, object, array, boolean or null
3. Syntax:
[ "Ford", "BMW", "Fiat" ]
4. JSON arrays are surrounded by square brackets [].
**Tip to remember** : Here, order of element is important. That means you have
to go straight like the shape of the bracket i.e. straight lines.
(Note :It is just my logic to remember the shape of both.)
5. Order of elements is important. Example: ["Ford","BMW","Fiat"] is not
equal to ["Fiat","BMW","Ford"]
6. JSON can store nested Arrays that are passed as a value.
JSON对象
1. JSON objects are written in key/value pairs.
2. Keys must be strings, and values must be a valid JSON data type (string, number,
object, array, boolean or null).Keys and values are separated by a colon.
Each key/value pair is separated by a comma.
3. Syntax:
{ "name":"Somya", "age":25, "car":null }
4. JSON objects are surrounded by curly braces {}
Tip to remember : Here, order of element is not important. That means you can go
the way you like. Therefore the shape of the braces i.e. wavy.
(Note : It is just my logic to remember the shape of both.)
5. Order of elements is not important.
Example: { rollno: 1, firstname: 'Somya'}
is equal to
{ firstname: 'Somya', rollno: 1}
6. JSON can store nested objects in JSON format in addition to nested arrays.