简体   繁体   English

清单 <JSONObject> vs JSONArray

[英]List<JSONObject> vs JSONArray

What is the difference between using List<JSONObject> and JSONArray ? 使用List<JSONObject>JSONArray什么区别?

This is the sort of structure I wish to loop through : [{"key1","value1"},{"key2","value2"},{"key3","value3"}] 我希望循环这种结构: [{"key1","value1"},{"key2","value2"},{"key3","value3"}]

List<JSONObject> gives me the flexibility of looping through using for-each loop, while JSONArray will give JSONException when using getJSONObject method. List<JSONObject>为我提供了使用for-each循环进行循环的灵活性,而JSONArray在使用getJSONObject方法时将提供JSONException Which is the better method here? 哪种方法更好?

They are almost same apart from one major difference. 除了一个主要区别之外,它们几乎相同。 If you look close at source code, JsonArray implements List . 如果您仔细看一下源代码, JsonArray实现List

So it is already a type of List 所以它已经是List

public interface JsonArray
extends JsonStructure, List<JsonValue> 

However the major difference is that, JsonArray is immutable . 但是主要的区别在于,JsonArray是immutable

https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html 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). JsonArray表示一个不变的JSON数组(零个或多个值的有序序列)。 It also provides an unmodifiable list view of the values in the array. 它还提供了数组中值的不可修改的列表视图。

Choose carefully if you want to have an immutable container of Json, use JsonArray. 如果要拥有一个不可变的Json容器,请仔细选择,请使用JsonArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM