How to convert JSON to a Java object

Source: Internet
Author: User

Currently, it is quite simple to convert a Java object to a JSON object. However, it is more complicated to convert a JSON object to a Java object.

The first method is JSON-lib.

The second method is 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 using GOOGLE's Gson. Anyone who has written about Android knows that this is a Google product. The biggest benefit is that it basically does not rely on other packages. It is naturally easy to use, and the value method is flexible. Complete the complicated JSON values.

Gson has two concepts. One is JsonObject and JsonArray. Specific Code

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 blank \" },{ \ "questionID \": \ "QSTest06 \", \ "anserContent \": \ "3 \" },{ \ "questionID \": \ "QSTest07 \", \ "anserContent \": \ "2.2 \"}]} "; JsonObject root = new JsonParser (). parse (jsonData ). getAsJsonObject (); System. out. println (root. get ("questionnaireID "). toString (); // directly obtain the root node value JsonArray AnswerList = root. getAsJsonArray ("answerResults"); // retrieves an 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 ());}}}


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.