FastJson的學習之JSON互相轉Map集合,List集合,JavaBean
阿新 • • 發佈:2018-11-23
https://blog.csdn.net/weixin_36910300/article/details/79182120
建立兩個實體類 一個是部門類,一個是部門員工類
部門員工類
public class Person { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; }public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() {return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
部門類
public class Dept { private String deptName; private List<Person> personList; public String getDeptName() {return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public List<Person> getPersonList() { return personList; } public void setPersonList(List<Person> personList) { this.personList = personList; } @Override public String toString() { return "Dept{" + "deptName='" + deptName + '\'' + ", personList=" + personList + '}'; } }
物件轉為json字串格式
JSON.toJSONString(Object object)
@Test public void fun1(){ List<Person> personArrayList = new ArrayList<Person>(); Person person = new Person(); person.setId(1); person.setName("張三"); person.setAge(18); System.out.println(JSON.toJSONString(person)); personArrayList.add(person); Person person1 = new Person(); person1.setId(2); person1.setName("李四"); person1.setAge(18); personArrayList.add(person1); System.out.println(JSON.toJSONString(person1)); Dept dept = new Dept(); dept.setDeptName("財務部"); dept.setPersonList(personArrayList); String jsonString = JSON.toJSONString(personArrayList); System.out.println(jsonString); }
控制檯的列印
JSON字串轉JavaBean
JSON.parseObject(String text,Class<T> clazz)
@Test public void fun2(){ String str1="{\"age\":18,\"id\":1,\"name\":\"張三\"}"; System.out.println(JSON.parseObject(str1,Person.class)); String str2="{\"age\":18,\"id\":2,\"name\":\"李四\"}"; System.out.println(JSON.parseObject(str2,Person.class)); }
控制檯列印
JSON字串陣列轉List集合
JSON.parseArray(String text, Class<T> clazz)
@Test public void fun3(){ String str="[{\"age\":18,\"id\":1,\"name\":\"張三\"},{\"age\":18,\"id\":2,\"name\":\"李四\"}]"; List<Person> PersonList=JSON.parseArray(str,Person.class); System.out.println(PersonList); }
控制檯列印
Map集合轉json
JSON.toJSONString(Object object)
@Test public void fun4(){ Map<String,Person> personMap=new HashMap<String, Person>(); Person person = new Person(); person.setId(1); person.setName("張三"); person.setAge(18); personMap.put("zhangsan",person); Person person1 = new Person(); person1.setId(2); person1.setName("李四"); person1.setAge(18); personMap.put("lisi",person1); String json = JSON.toJSONString(personMap); System.out.println(json); }
控制檯列印的資訊
MapJson字串轉Map集合
(Map)JSON.parse(Object object)
@Test public void fun5(){ String str="{\"lisi\":{\"age\":18,\"id\":2,\"name\":\"李四\"},\"zhangsan\":{\"age\":18,\"id\":1,\"name\":\"張三\"}}"; Map<String,Person> map = (Map)JSONObject.parse(str); System.out.println(map.get("lisi")); System.out.println(map.get("zhangsan")); }
控制檯的列印輸出