JSON object to convert Java object

Source: Internet
Author: User

Public static void main (String[] args)  {string studentjson= "{\" username\ ": \" Xiaoming\ ", \" age\ ": 20,\" desc\ ": \" good student \ "}"; string studentjson2= "{\" username\ ": \" xiaohong\ ", \" age\ ": 20,\" desc\ ": \" good student \ "}"; String[] json={studentjson,studentjson2};       //student st= ( Student)  json2bean (studentjson, student.class);        jsonarray  jsonarray=jsonarray.fromobject (JSON);//json objects hold a lot of student data        for ( Object obj:jsonarray) {       student st= (Student)  json2Bean ( ((Jsonobject)  obj). ToString (),  student.class);        System.out.println (St.getusername () + ":" +st.getdesc ());       }}    public static object json2bean (STRING JSON,CLASS CLA) {JSONObject  Jsonobject=jsonobject.fromobjeCT (JSON); Return jsonobject.tobean (JSONOBJECT, CLA);} 

Using the Json-lib framework, you can convert the foreground JSON object into a Java object, you need the following jar package, you can put the JSON data structure of multiple Java objects in the database, the use of Jsonarray into a JSON array, It then iterates through the array to get the Java object corresponding to each JSON data, and calls Jsonobject's Tobean method to get the object instance

JSON object to convert Java object

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.