JSONObject 和 JSONArray 的区别 [英] Difference between JSONObject and JSONArray
问题描述
在 Google 上短暂浏览后,我发现了这个 link 描述了差异,但从语法的角度来看.
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?
推荐答案
当您在 Android 中处理 JSON 数据时,您将使用 JSONArray
来解析以数组括号开头的 JSON.JSON 中的数组用于组织相关项目的集合(可能是 JSON 对象).
例如:[{"name":"item 1"},{"name": "item2} ]
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} ]
另一方面,在处理以花括号开头的 JSON 时,您将使用 JSONObject
.JSON 对象通常用于包含与一项相关的键/值对.例如:{"name": "item1", "description":"a JSON object"}
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"}
当然,JSON 数组和对象可以相互嵌套.一个常见的例子是一个 API,它返回一个 JSON 对象,其中包含一些元数据以及与您的查询匹配的项目数组:
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"} ]}
这篇关于JSONObject 和 JSONArray 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!