FastJson
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
package apitest; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.github.checkpoint.JsonPath; import test.User; public class TestTest { public static void main(String[] args) { //对象转json User user =new User(1,"zhangsan","男","生日","地址"); //SerializerFeature对数据进行一些定制 常用的有格式、空的时候展示、单引号等等 String jsonString = JSON.toJSONString(user,SerializerFeature.PrettyFormat, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.UseSingleQuotes); System.out.println(jsonString); //json转对象 User user1 =JSON.parseObject(jsonString,User.class); System.out.println(user1); //json转对象map Map map=JSON.parseObject(jsonString,HashMap.class); System.out.println(map); //map转json String jsonString2=JSON.toJSONString(map); System.out.println(jsonString2); } }