Eri*_*ine 169
当您在Android中使用JSON数据时,您将使用JSONArray解析以数组括号开头的JSON.JSON中的数组用于组织相关项的集合(可以是JSON对象).
例如:[{"name":"item 1"},{"name": "item2} ]
另一方面,您将JSONObject在处理以花括号开头的JSON时使用.JSON对象通常用于包含与一个项相关的键/值对.例如:{"name": "item1", "description":"a JSON object"}
当然,JSON数组和对象可以彼此嵌套.一个常见的例子是API,它返回一个JSON对象,其中包含一些元数据以及与您的查询匹配的项目数组:
{"startIndex": 0, "data": [{"name":"item 1"},{"name": "item2"} ]}
Run Code Online (Sandbox Code Playgroud)
Ale*_*zin 93
差异与(哈希)地图与列表相同.
的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"}
]}
Run Code Online (Sandbox Code Playgroud)
Yog*_*thi 22
最好以编程方式理解.
当语法是
{}这时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.
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我总是使用对象,它更容易扩展,JSON数组不是.例如,您最初将某些数据作为json数组,然后您需要在其上添加一个状态标题,除非您将数据嵌套在对象中,否则它会有点卡住.唯一的缺点是创建/解析的复杂性略有增加.
而不是
[datum0, datum1, datumN]
Run Code Online (Sandbox Code Playgroud)
你有
{data: [datum0, datum1, datumN]}
Run Code Online (Sandbox Code Playgroud)
然后你可以添加更多......
{status: "foo", data: [datum0, datum1, datumN]}
Run Code Online (Sandbox Code Playgroud)
两者的使用取决于数据的结构。
简而言之,如果您计划优先考虑主键等唯一标识符,则可以使用嵌套对象方法。
例如:
{
"Employees" : {
"001" : {
"Name" : "Alan",
"Children" : ["Walker", "Dua", "Lipa"]
},
"002" : {
"Name" : "Ezio",
"Children" : ["Kenvey", "Connor", "Edward"]
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您打算存储一组不需要唯一标识的值,请使用数组优先方法。
例如:
{
"Employees":[
{
"Name" : "Alan",
"Children" : ["Walker", "Dua", "Lipa"]
},
{
"Name" : "Ezio",
"Children" : ["Kenvey", "Connor", "Edward"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
尽管您可以使用带有标识符的第二种方法,但在某些情况下查询和理解可能会更困难或太复杂。此外,根据数据库,人们可能必须应用一种合适的方法。例如:MongoDB / Firebase
| 归档时间: |
|
| 查看次数: |
121301 次 |
| 最近记录: |