The first method is to use JSON-lib.
The second method is to use JACKSON.
The first two methods are relatively easy for relatively simple Pojo objects. However, compared with nested multi-layer data, the complexity is directly increased.
The third method is solved by using GOOGLE's Gson. Anyone who has written Android knows that this thing is produced by Google. The biggest advantage is that it basically does not rely on other packages. It is natural and refreshing to use, and the value method is very flexible. All the complex JSON values are basically done.
There are two concepts in Gson. One is JsonObject and JsonArray. Depend on the code specifically
The code copy is as follows:
package com.mycompany.gsondata;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
String jsonData = "{/"questionnaireID/": /"QNTest/",/"answerResults/":[{/"questionID/":/"QSTest01/",/"anserContent/":/"cfb7f441-9086-11e3-8cf8-000c2945c442/"},{/" questionID/":/"QSTest01/",/ "anserContent/":/"cfb7f441-9086-11e3-8cf8-000c2945c442/"},{/"questionID/":/"QSTest03/",/"anserContent/":/"6b3a9cce-9087-11e3-8cf8-000c2945c442 ,a0863 31d-9087-11e3-8cf8-000c2945c442/"},{/"questionID/":/"QSTest01/",/"anserContent/":/"cfb7f441-9086-11e3-8cf8-000c2945c442/"},{/" questionID/":/"QSTest0 5/",/"anserContent/":/"test test text fill-in-the-blanks/"},{/"questionID/":/"QSTest06/",/"anserContent/":/"3/"},{/"questionID /":/"QSTest07/",/"anserContent/":/"2.2/"}]}";
JsonObject root = new JsonParser().parse(jsonData).getAsJsonObject();
System.out.println(root.get("questionnaireID").toString());//The root node value is taken directly
JsonArray AnswerList = root.getAsJsonArray("answerResults");//Get the array
for (int i = 0; i < AnswerList.size(); i++) {
System.out.println(AnswerList.get(i).getAsJsonObject().get("questionID").toString());
System.out.println(AnswerList.get(i).getAsJsonObject().get("anserContent").toString());
}
}
}