The first method uses Json-lib.
The second method, using JACKSON.
The first two methods are easier for relatively simple pojo objects. But as opposed to nesting multiple layers of data, complexity goes directly up.
The third method, using Google's Gson to solve the problem. As the android knows, it's Google, and the best thing about it is that it doesn't depend on other packages. The use of natural is very cool, the value of the way is very flexible. For the complex JSON value, basically all done.
There are two kinds of concepts in Gson. One is Jsonobject and Jsonarray. Specific look at the code
Copy Code code 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,a086331d-9087-11e3-8cf8-000c2945c442\ "},{\" questionID\ ": \" QSTest01\ ", \" Ansercontent\ ": \ cfb7f441-9086-11e3-8cf8-000c2945c442\"},{\ "questionid\": \ "qstest05\", \ "ansercontent\": \ " 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 ());//Direct fetch of the root node value
Jsonarray answerlist = Root.getasjsonarray ("Answerresults");//Fetch 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 ());
}
}
}