使用fastjson字符串对象互转

一、准备

1、导入fastjson包


    com.alibaba
    fastjson
    1.2.76

2、创建测试对象

animal对象:

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Animal {

    private String name;

    private Integer age;
}

Zoo对象:

import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@Data
@ToString
public class Zoo {

    private String name;

    private List animalList = new ArrayList<>();

}

二、开始数据类型转换

1、对象转json

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);
System.out.println("dog对象转json之前:" + dog);
String dogJson = JSON.toJSONString(dog);
System.out.println("dog对象转json之后:" + dogJson);

输出结果:
在这里插入图片描述
2、json转对象

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);
String dogJson = JSON.toJSONString(dog);
Animal parseDog = JSON.parseObject(dogJson, Animal.class);
System.out.println("json转对象,parseDog:" + parseDog);

输出结果:
在这里插入图片描述
3、list对象转json

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

List animalList = new ArrayList<>();
animalList.add(dog);
animalList.add(cat);
String jsonString = JSON.toJSONString(animalList);
System.out.println("list对象转json jsonString:" + jsonString);

输出结果:
在这里插入图片描述
4.1 json转list (方法一)

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

List animalList = new ArrayList<>();
animalList.add(dog);
animalList.add(cat);
String jsonString = JSON.toJSONString(animalList);
List arrayList = JSON.parseArray(jsonString, Animal.class);
System.out.print("json转list对象:");
arrayList.forEach(System.out::print);

输出结果:
在这里插入图片描述
4.2 json转list (方法二)

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

List animalList = new ArrayList<>();
animalList.add(dog);
animalList.add(cat);
String jsonString = JSON.toJSONString(animalList);
System.out.print("json转list对象:");
List animals = JSON.parseObject(jsonString,new TypeReference>(){}); 
animals.forEach(System.out::print);

输出结果:
在这里插入图片描述

5、Java复杂对象(含list对象集合)转json

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Zoo zoo = new Zoo();
zoo.setName("tianjingdihua");
zoo.getAnimalList().add(dog);
zoo.getAnimalList().add(cat);
String zonJson = JSON.toJSONString(zoo);
System.out.println("zoo对象转json之后:" + zonJson);

输出结果:
在这里插入图片描述

6、json转Java复杂对象(含list对象集合)

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Zoo zoo = new Zoo();
zoo.setName("tianjingdihua");
zoo.getAnimalList().add(dog);
zoo.getAnimalList().add(cat);
String zonJson = JSON.toJSONString(zoo);

Zoo parseZoo = JSON.parseObject(zonJson, Zoo.class);
System.out.println("json转Java复杂对象parseZoo:" + parseZoo);

输出结果:
在这里插入图片描述
7、map转json

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Map map = new HashMap<>();
map.put("dog", dog);
map.put("cat", cat);
String json = JSON.toJSONString(map, true);
System.out.println("map转json:" + json);

输出结果prettyForm:
使用fastjson字符串对象互转_第1张图片
8、json转map

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Map map = new HashMap<>();
map.put("dog", dog);
map.put("cat", cat);
String json = JSON.toJSONString(map, true);
Map animalMap = (Map)JSON.parse(json);
animalMap.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));

输出结果:
在这里插入图片描述
9、数组转json

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Animal[] animals = new Animal[2];
animals[0] = dog;
animals[1] = cat;

String jsonString = JSON.toJSONString(animals);
System.out.println("数组转json jsonString:" + jsonString);

输出结果:
在这里插入图片描述
10、json转对象列表

Animal dog = new Animal();
dog.setName("dog");
dog.setAge(1);

Animal cat = new Animal();
cat.setName("cat");
cat.setAge(1);

Animal[] animals = new Animal[2];
animals[0] = dog;
animals[1] = cat;

String jsonString = JSON.toJSONString(animals);
List animalList = JSON.parseArray(jsonString, Animal.class);
System.out.println("animalList:" + animalList);

输出结果:
在这里插入图片描述

Mark

你可能感兴趣的:(Java,java)