标签:ide com system 依赖 序列 alibaba log class int
转自 https://blog.csdn.net/suprezheng/article/details/86500747
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
<!--阿里巴巴 fastjson依赖--> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> 测试过程及其代码: package cn.mesmile.json; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.junit.Test; import java.util.Arrays; import java.util.List; /** * @Created with IDEA * @author: Super Zheng * @Description: fastjson测试 * @Date:2019/1/15 * @Time:22:34 */ public class FastJsonTest { @Test public void test () {
// 1.测试将一个【对象】转换成【json字符串】 String jsonString = JSONObject.toJSONString(new Person("测试1",18)); System.out.println(jsonString); // 2.测试将一个【json字符串】转换成【对象】 Person person1 = JSONObject.parseObject(jsonString, Person.class); System.out.println(person1); // 3.测试将【多个对象】转换成【json字符串数组】 String jsonString1 = JSONArray.toJSONString(Arrays.asList(new Person("测试2",19), new Person("测试3", 20))); System.out.println(jsonString1); // 4.测试将【josn字符串数组】转换成多个对象 List<Person> people = JSONArray.parseArray(jsonString1, Person.class); System.out.println(people); } }
package cn.mesmile.json; /** * @Created with IDEA * @author: Super Zheng * @Description: 实体类 * @Date:2019/1/15 * @Time:22:43 */ public class Person { private String name; private Integer age; 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; } public Person(){} public Person(String name,Integer age){ this.name=name; this.age=age; } @Override public String toString() { return "Person{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
标签:ide com system 依赖 序列 alibaba log class int
原文地址:https://www.cnblogs.com/shipengda/p/14049742.html