[英]How to parse JSON data array with random key names using Java Spring Webclient?
[英]How to access key:values in JSON using Java with no array names
可以說我有一個像這樣的JSON數組
[{"a":"1", "b":"2"}, {"c":"3", "d":"4"}]
我正在嘗試像這樣在Java中獲取它
JSONArray array = new JSONArray(responseBody); //resposeBody is the JSON array
for (int i = 0; i < array.length(); i++) {
String arr = array.get(i).toString(); //Trying to get each array like this
JSONArray json = new JSONArray(arr);
}
在JSONArray json = new JSONArray(arr);
行中JSONArray json = new JSONArray(arr);
我得到錯誤
A JSONArray text must start with '['
如何訪問這些值?
編輯:我的意思是我如何獲取每個數組及其值
做就是了 :
JSONObject myJsonObject = new JSONObject(jsonString);
在數組中:
JSONObject myjObject = myJsonArray.getJSONObject(i);
您的JSONArray是JSONObjects的數組。 你可以試試
JSONArray array = new JSONArray(responseBody);
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
}
現在,您可以進一步解析各個JSONObject。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.