Gson Converter Android

Source: Internet
Author: User
Tags string to json tojson

Introducing the Gson package//Gson parsing. Encapsulating server returns JSON resultsGson Gson =NewGson ();//1. Object Turn StringStudent stu =NewStudent (); Stu.setstudentid (333); Stu.setstudentname ("QQQ"); String Stustr=Gson.tojson (STU); System.out.println (STUSTR); //{"Studentname": "QQQ", "StudentID": 333}//String Goto ObjectStudent user2 = Gson.fromjson (Stustr, Student.class);           System.out.println (User2); String stutemp= "{\" studentname\ ": \" qqq2\ ", \" studentid\ ": 3335}"; Student User4= Gson.fromjson (Stutemp, Student.class);     System.out.println (USER4); //2. Object list Turn stringList<student> testbeanlist =NewArraylist<student>(); Student Testbean=NewStudent (); Testbean.setstudentid (555); Testbean.setstudentname ("552");           Testbeanlist.add (Testbean); //Gson gsonlist = new Gson (); Type type =NewTypetoken<list<student>> () {}.gettype ();//specifying Collection Object PropertiesString Beanlisttojson =Gson.tojson (testbeanlist, type); System.out.println (Beanlisttojson); //[{"Studentname": "552", "StudentID": 555}]//collection string to Object listList<student> Testbeanlistfromjson =Gson.fromjson (Beanlisttojson, type); System.out.println (Testbeanlistfromjson); //[555:552]//3, set if the type is not specified by default to stringList<string> testlist =NewArraylist<string>(); Testlist.add ("First"); Testlist.add ("Second"); String Listtojson=Gson.tojson (testlist); System.out.println (Listtojson); //["First", "second"]//The collection string goes back to the specified typeList<string> TestList2 = (list<string>) Gson.fromjson (Listtojson,NewTypetoken<list<string>>() {}.gettype ());     System.out.println (TESTLIST2); //4. Convert the HashMap string to JSONmap<string, string> TestMap =NewHashmap<string, string>(); Testmap.put ("id", "Id.first"); Testmap.put ("Name", "Name.second"); String Maptojson=Gson.tojson (TestMap); System.out.println (Maptojson); //{"id": "Id.first", "name": "Name.second"}//HashMap Turn Objectsmap<string, string> userMap2 = (map<string, string>) Gson.fromjson (Maptojson,NewTypetoken<map<string, string>>() {}.gettype ()); System.out.println (USERMAP2); //{id=id.first, name=name.second}//5. Objects containing common objects, sets, map conditionsStudent user1 =NewStudent (); User1.setstudentid (1001); User1.setstudentname ("Zhang San"); Student User3=NewStudent (); User3.setstudentid (1002); User3.setstudentname ("John Doe"); Map<string, student> UserMap =NewHashmap<string, student>(); Usermap.put ("User1", User1); Usermap.put ("User3", User3); List<Student> userlist =NewArraylist<student>();           Userlist.add (user1);                     Userlist.add (USER3); Teacher Groupbean=NewTeacher ();          Groupbean.setstudent (user1);          Groupbean.setstus (userlist);          Groupbean.setmap ((HASHMAP) usermap); //groupbean.setuserlist (userlist); Gson Gsongroup =NewGson (); String Sgroupbean= Gsongroup.tojson (Groupbean,NewTypetoken<teacher>() {}.gettype ());           System.out.println (Sgroupbean); /*{"Stus": [{"Studentname": "Zhang San", "StudentID": 1001},{"Studentname": "John Doe", "StudentID": 1002}], "student": {" Studentname ":" Zhang San "," StudentID ": 1001}," map ": {" User3 ": {" Studentname ":" John Doe "," StudentID ": 1002}," user1 ": {" Studentname ":" Zhang San "," StudentID ": 1001}," id ": 0," Age ": 0}*/      //Complex object string to objectTeacher groupBean2 =(Teacher) Gson.fromjson (Sgroupbean,NewTypetoken<teacher>() {}.gettype ());  System.out.println (GROUPBEAN2); 

Gson Converter Android

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.