使用List<JSONObject>
有什么区别和JSONArray
?
这是我希望循环的结构:[{"key1","value1"},{"key2","value2"},{"key3","value3"}]
List<JSONObject>
为我提供了使用 for-each 循环进行循环的灵 active ,而 JSONArray
将给出 JSONException
当使用getJSONObject
时方法。哪种方法更好?
最佳答案
除了一个主要区别之外,它们几乎相同。如果您仔细查看源代码,您会发现 JsonArray
实现了 List
。
所以它已经是一种List
public interface JsonArray
extends JsonStructure, List<JsonValue>
但主要区别在于,JsonArray 是不可变
。
https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array.
如果你想要一个不可变的 Json 容器,请仔细选择,使用 JsonArray。
关于java - List<JSONObject> 与 JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680305/