Gson轉換器 android

來源:互聯網
上載者:User

標籤:android   style   blog   ar   color   sp   on   div   log   

引入gson包//Gson解析.封裝伺服器返回json結果   Gson gson = new Gson();//1、對象轉string          Student stu = new Student();          stu.setStudentId(333);          stu.setStudentName("qqq");          String stuStr = gson.toJson(stu);          System.out.println(stuStr); //{"studentName":"qqq","studentId":333}                      //    string轉對象          Student 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、對象List轉string          List<Student> testBeanList = new ArrayList<Student>();           Student testBean = new Student();           testBean.setStudentId(555);          testBean.setStudentName("552");          testBeanList.add(testBean);           //Gson gsonList = new Gson();           Type type = new TypeToken<List<Student>>(){}.getType();  //指定集合對象屬性          String beanListToJson = gson.toJson(testBeanList, type);           System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]           //集合string轉對象list          List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);           System.out.println(testBeanListFromJson); //[555:552]    //3、集合如果不指定類型 預設為String          List<String> testList = new ArrayList<String>();           testList.add("first");           testList.add("second");           String listToJson = gson.toJson(testList);           System.out.println(listToJson); //["first","second"]                   //集合字串轉回來需要指定類型          List<String> testList2 = (List<String>) gson.fromJson(listToJson,             new TypeToken<List<String>>() {             }.getType());           System.out.println(testList2);     //4、 將HashMap字串轉換為 JSON           Map<String, String> testMap = new HashMap<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轉對象          Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,                   new TypeToken<Map<String, String>>() {               }.getType());           System.out.println(userMap2); //{id=id.first, name=name.second}     //5、對象含有普通對象、集合、map情況          Student user1 = new Student();           user1.setStudentId(1001);           user1.setStudentName("張三");                     Student user3 = new Student();           user3.setStudentId(1002);           user3.setStudentName("李四");                     Map<String, Student> userMap = new HashMap<String, Student>();           userMap.put("user1", user1);           userMap.put("user3", user3);                     List<Student> userList = new ArrayList<Student>();           userList.add(user1);           userList.add(user3);                     Teacher groupBean = new Teacher();           groupBean.setStudent(user1);          groupBean.setStus(userList);          groupBean.setMap((HashMap)userMap);          //groupBean.setUserList(userList);           Gson gsonGroup = new Gson();             String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {               }.getType());           System.out.println(sGroupBean);           /*{"stus":[{"studentName":"張三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"張三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"張三","studentId":1001}},"id":0,"age":0}*/      //複雜物件string轉對象     Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,      new TypeToken<Teacher>() {      }.getType());     System.out.println(groupBean2);  

 

Gson轉換器 android

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.