Ways to convert JSON data into Java objects _java

Source: Internet
Author: User
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 ());
}

}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.