【发布时间】:2012-08-30 16:03:32
【问题描述】:
在浏览了 Google 之后,我发现这个 link 描述了差异,但从语法的角度来看。
在编程场景中,什么时候会优先选择一个?
【问题讨论】:
在浏览了 Google 之后,我发现这个 link 描述了差异,但从语法的角度来看。
在编程场景中,什么时候会优先选择一个?
【问题讨论】:
当您在 Android 中处理 JSON 数据时,您将使用 JSONArray 来解析以数组括号开头的 JSON。 JSON 中的数组用于组织相关项目的集合(可以是 JSON 对象)。
例如:[{"name":"item 1"},{"name": "item2} ]
另一方面,在处理以花括号开头的 JSON 时,您将使用 JSONObject。 JSON 对象通常用于包含与一项相关的键/值对。
例如:{"name": "item1", "description":"a JSON object"}
当然,JSON 数组和对象可以相互嵌套。一个常见的例子是一个 API,它返回一个 JSON 对象,其中包含一些元数据以及与您的查询匹配的项目数组:
{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]}
【讨论】:
区别与(哈希)映射与列表相同。
JSONObject:
{ID : 1}
{id: 1, name: 'B'} 的 JSONObject 等于 {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
JSONObject 是一个类似 JSON 的对象,可以表示为 JSONArray 中的一个元素。 JSONArray 可以包含一个(或多个)JSONObject
希望对你有帮助!
【讨论】:
我总是使用对象,它更容易扩展,JSON 数组不是。例如,您最初有一些数据作为 json 数组,然后您需要在其上添加状态标头,除非您将数据嵌套在对象中,否则您会有点卡住。唯一的缺点是稍微增加了创建/解析的复杂度。
所以不是
[datum0, datum1, datumN]
你应该有
{data: [datum0, datum1, datumN]}
然后你可以添加更多...
{status: "foo", data: [datum0, datum1, datumN]}
【讨论】:
为了更容易理解,下面是 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.
【讨论】:
两者的使用取决于数据的结构。
简单地说, 如果您打算优先考虑诸如主键之类的唯一标识符,则可以使用嵌套对象方法。
例如:
{
"Employees" : {
"001" : {
"Name" : "Alan",
"Children" : ["Walker", "Dua", "Lipa"]
},
"002" : {
"Name" : "Ezio",
"Children" : ["Kenvey", "Connor", "Edward"]
}
}
或者, 如果您打算存储一组值而无需唯一标识,请使用 Array first 方法。
例如:
{
"Employees":[
{
"Name" : "Alan",
"Children" : ["Walker", "Dua", "Lipa"]
},
{
"Name" : "Ezio",
"Children" : ["Kenvey", "Connor", "Edward"]
}
]
}
虽然您可以使用带有标识符的第二种方法,但在某些情况下查询和理解可能更难或太复杂。此外,根据数据库,可能必须采用合适的方法。 例如:MongoDB/Firebase
【讨论】:
当 JSON 以 {} 开头时,它是一个 Object JSON object,当它以 [] 开头时,它是一个数组 JSON Array。
一个 JSON 数组可以包含许多对象,称为对象数组。
【讨论】:
我知道,之前的所有答案都对您的问题很有见地。在找到这个 SO 线程前一分钟,我也和你一样困惑。在阅读了一些答案之后,我得到了以下信息: JSONObject 是一个类似 JSON 的对象,可以表示为数组 JSONArray 中的一个元素。换句话说,一个 JSONArray 可以包含一个(或多个)JSONObject。
【讨论】: