Sometimes we need to convert the json string into an array, and only take some attribute values for the assignment of the next method, which involves the conversion of the json string,
There are two forms of json string, one is {A,B,C}, and the other is [{A},{B},{C}]. But many times it is a combination of the two.
First import the jar package
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
If it is the first type, you only need to add the following code, note that str is a json string
JSONObject json=(JSONObject)JSONObject.toJSON(JSON.parse(str));
System.out.println("facesets:"+json.getString("facesets"));
is the second with the following code
JSONArray temp=JSONArray.parseArray(json.getString("facesets"));
for(int i=0;i<temp.size();i++){
JSONObject obj=(JSONObject)temp.get(i);
System.out.println(obj.getString("faceset_token"));
}
Often it is a combination of the two
JSONObject json=(JSONObject)JSONObject.toJSON(JSON.parse(str));
System.out.println("facesets:"+json.getString("facesets"));
JSONArray temp=JSONArray.parseArray(json.getString("facesets"));
for(int i=0;i<temp.size();i++){
JSONObject obj=(JSONObject)temp.get(i);
System.out.println(obj.getString("faceset_token"));
}
You can look at the output
Hope it helps you