我从 Java 应用程序提交了一个查询,该查询在 Elasticsearch 服务器上运行时以字符串形式返回结果。我希望结果为 JSONObject
列表对象。我可以将字符串转换为 JSONObject
使用JSONObject jsonResponse = new JSONObject(responseString)
.
有什么方法可以让我以 List<JSONObject>
的形式得到它吗? ?
最佳答案
您可以使用 JSONArray 而不是使用 JSONObject 。如果您确实需要将其转换为列表,您可以执行以下操作:
List<JSONObject> list = new ArrayList<JSONObject>();
try {
int i;
JSONArray array = new JSONArray(string);
for (i = 0; i < array.length(); i++)
list.add(array.getJSONObject(i);
} catch (JSONException e) {
System.out.println(e.getMessage());
}
关于java - 将 JSONObject 转换为 List<JSONObject> 或将字符串转换为 List<JSONObject>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31649495/