1、数组和json的转换
1 package com.test.gson; 2 import com.google.gson.Gson; 3 public class ArrayToJson { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 int[] numbers = {1,2,3,4,5,6,7,8,9}; 8 String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; 9 10 Gson gson = new Gson(); 11 12 String numbersJson = gson.toJson(numbers); 13 System.out.println("numbersJson=" + numbersJson); 14 15 int[] nums = gson.fromJson(numbersJson, int[].class); 16 for(int i=0; i<nums.length ; i++){ 17 System.out.println(nums[i]); 18 } 19 String daysJson = gson.toJson(days); 20 System.out.println("daysJson=" + daysJson); 21 22 String[] weekDays = gson.fromJson(daysJson, String[].class); 23 for(int j=0; j<weekDays.length; j++){ 24 System.out.println(weekDays[j]); 25 } 26 } 27 28 }
2、对象和json的转换
1 package com.test.gson; 2 3 import com.google.gson.Gson; 4 5 public class StudentToJson { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Student student = new Student(1001,"gulu","beijing"); 10 Gson gson = new Gson(); 11 12 String studentJson = gson.toJson(student); 13 System.out.println("studentJson=" + studentJson); 14 15 Student stu = gson.fromJson(studentJson, Student.class); 16 System.out.println(stu.toString()); 17 System.out.println(stu.getId()); 18 System.out.println(stu.getName()); 19 System.out.println(stu.getAddress()); 20 } 21 22 }
3、List和json的转换
1 package com.test.gson; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.lang.reflect.Type; 6 7 import com.google.gson.Gson; 8 import com.google.gson.reflect.TypeToken; 9 10 public class CollectionToJson { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 List<String> names = new ArrayList<String>(); 15 names.add("Alice"); 16 names.add("Bob"); 17 names.add("Carol"); 18 names.add("Mallory"); 19 20 Gson gson = new Gson(); 21 22 String jsonNames = gson.toJson(names); 23 System.out.println("jsonNames="+jsonNames); 24 25 Student a = new Student(1,"zhangsan","beijing"); 26 Student b = new Student(2,"wangwu","nanjing"); 27 Student c = new Student(3,"lisi","guangzhou"); 28 Student d = new Student(4,"wangba","shanghai"); 29 30 List<Student> students = new ArrayList<Student>(); 31 students.add(a); 32 students.add(b); 33 students.add(c); 34 students.add(d); 35 36 String jsonStudents = gson.toJson(students); 37 System.out.println("jsonStudents="+jsonStudents); 38 39 Type type = new TypeToken<List<Student>>(){}.getType(); 40 List<Student> studentList = gson.fromJson(jsonStudents, type); 41 for(Student student : studentList){ 42 System.out.println(student.getName()); 43 } 44 } 45 46 }
4、map和json的转换
1 package com.test.gson; 2 3 import java.lang.reflect.Type; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import com.google.gson.Gson; 8 import com.google.gson.reflect.TypeToken; 9 10 public class MapToJson { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Map<String, String> colours = new HashMap<String,String>(); 15 colours.put("black", "#000000"); 16 colours.put("red", "#ff0000"); 17 colours.put("green", "#00ff00"); 18 colours.put("blue", "#0000ff"); 19 20 Gson gson = new Gson(); 21 String mapJson = gson.toJson(colours); 22 System.out.println("gson="+mapJson); 23 24 Type type = new TypeToken<Map<String,String>>(){}.getType(); 25 26 Map<String,String> map = gson.fromJson(mapJson, type); 27 for(String key : map.keySet()){ 28 System.out.println("map.get=" + map.get(key)); 29 } 30 } 31 32 }
时间: 2024-11-09 16:24:54