[英]How to retrieve json array elements from JSON object in Java?
JSONObject firstObject = (JSONObject) jsonParser.parse(new FileReader(firstNamesPath));
I have this JSONObject, and I want to be able to access elements in the array inside of it.我有这个 JSONObject,我希望能够访问其中的数组中的元素。 The object opens successfully, I just don't know how to access the array called "firstNames".
对象成功打开,我只是不知道如何访问名为“firstNames”的数组。 It is in a file, and the object looks like this.
它在一个文件中,对象看起来像这样。
{
"firstNames": [
"Aaron",
"Abigail",
"Albert",
"Bob"
]
}
Edit: I am using org.json.simple.JSONObject .编辑:我正在使用 org.json.simple.JSONObject 。 If this is not recommended, I am more than willing to change it.
如果不建议这样做,我非常愿意更改它。
There are several ways to retrieve the json array value:有几种方法可以检索 json 数组值:
Assume we have a jsonString假设我们有一个 jsonString
jsonString = "{\n" + " \"firstNames\": [ \n" + " \"Aaron\",\n" + " \"Abigail\",\n" + " \"Albert\",\n" + " \"Bob\"\n" + " ]\n" + "}";
(since many classes share similar names, I am using the groupId and artifactId for distinction.) (由于许多类名称相似,因此我使用 groupId 和 artifactId 进行区分。)
Simple cases: use generic JSONObjects and JSONArrays.简单案例:使用通用 JSONObjects 和 JSONArrays。
json-simple (which OP is using) json-simple website , maven : json-simple (OP 正在使用) json-simple 网站, maven :
org.json.simple.parser.JSONParser jsonParser = new org.json.simple.parser.JSONParser();
org.json.simple.JSONObject firstObject = (org.json.simple.JSONObject) jsonParser.parse(jsonString);
org.json.simple.JSONArray jsonArray = (org.json.simple.JSONArray) firstObject.get("firstNames");
System.out.println(jsonArray);
JSON in Java (mentioned in adendrata 's answer): JSON-Java doc , maven Java 中的 JSON (在adendrata的回答中提到): JSON-Java doc , maven
org.json.JSONObject secondObject = new org.json.JSONObject(jsonString);
org.json.JSONArray jsonArray2 = secondObject.getJSONArray("firstNames");
System.out.println(jsonArray2);
gson : Gson , maven gson : gson ,行家
com.google.gson.JsonObject thirdObject = com.google.gson.JsonParser.parseString(jsonString).getAsJsonObject();
System.out.println(thirdObject.get("firstNames").getAsJsonArray());
For more complicated use cases, if you'd like to define your own class, and want to deserialize JSON string to your class, then you can use Gson or Jackson :对于更复杂的用例,如果您想定义自己的类,并且想将 JSON 字符串反序列化到您的类,那么您可以使用Gson或Jackson :
// Create your own class:
/*
public class YourOwnClass {
private List<String> firstNames;
public List<String> getFirstNames() {
return firstNames;
}
}
*/
Gson gson = new Gson();
YourOwnClass customObject1 = gson.fromJson(jsonString, YourOwnClass.class);
System.out.println(customObject1.getFirstNames());
ObjectMapper mapper = new ObjectMapper();
YourOwnClass customObject2 = mapper.readValue(jsonString, YourOwnClass.class);
System.out.println(customObject2.getFirstNames());
you can use JSONArray
to get array type of Json and looping to access each index您可以使用
JSONArray
获取 Json 的数组类型并循环访问每个索引
example:例子:
JSONArray array = firstObject.getJSONArray("firstNames");
for (int i = 0; i < array.length(); i++) {
System.out.println("Hello i'm " + array.get(i));
}
Try to use this : com.alibaba.fastjson.JSONObject
, and here's the dependency:尝试使用这个:
com.alibaba.fastjson.JSONObject
,这是依赖项:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
Then you can directly use the getJSONArray
method the answer shown above.然后你可以直接使用上面显示的答案的
getJSONArray
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.