平时用到json格式的数据有很多,今天抽个空整理一下.放眼望去除了原生JSONObject. json框架基本就是Gson,Fastjson,Jackson常用的会比较多一些. 下面就分别整理了用着三个框架的json和对象/集合的互转
测试环境
java Eclipse maven project main test
使用框架
我用的maven项目,所以配置pom.xml文件,写入以下配置即可. 需要jar包导入的自行下载.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
Jackson
public static void jackson() {
System.out.println("Jackson的使用: ");
ObjectMapper mapper = new ObjectMapper();
Person person = new Person();
person.setId(1);
person.setName("Roy");
person.setPassword("Roy123");
List<Person> persons = new ArrayList();
Person person1 = null;
for(int i = 0; i<2;i++) {
person1 = new Person();
person1.setId(i);
person1.setName("Andy"+i);
person1.setPassword("Andy123"+i);
persons.add(person1);
}
try {
System.out.println("对象转json: "+mapper.writeValueAsString(person));
System.out.println("集合转json: "+mapper.writeValueAsString(persons));
System.out.println("----------------------------------");
System.out.println("格式化jsonObject: "+mapper.readValue(mapper.writeValueAsString(person), Person.class));
System.out.println("格式化jsonListObject: "+mapper.readValue(mapper.writeValueAsString(persons), new TypeReference<List<Person>>() {}));
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用jackson互转可以看出来很方便. jackson已经为我们封装好了ObjectMapper. 不管是对象还是集合数据用writeValueAsString就可以直接转成json. 那么格式化json也是一句话readValue.就可以了.
深入看底层代码,这里跑出了异常.所以我们在使用的时候也要捕捉这个异常.
所以在代码中可以看到有3个catch
Gson
public static void gson() {
System.out.println("\n\nGson的使用: ");
Gson gson = new Gson();
Person person = new Person();
person.setId(1);
person.setName("Roy");
person.setPassword("Roy123");
List<Person> persons = new ArrayList();
Person person1 = null;
for(int i = 0; i<2;i++) {
person1 = new Person();
person1.setId(i);
person1.setName("Andy"+i);
person1.setPassword("Andy123"+i);
persons.add(person1);
}
System.out.println("对象转json: "+gson.toJson(person));
System.out.println("集合转json: "+gson.toJson(persons));
System.out.println("----------------------------------");
Person jsonObiect = gson.fromJson(gson.toJson(person), Person.class);
List<Person> jsonListObject = gson.fromJson(gson.toJson(persons), new TypeToken<List<Person>>(){}.getType());
System.out.println("格式化jsonObject: "+jsonObiect);
System.out.println("格式化jsonListObject: "+jsonListObject);
}
使用gson转json也是非常方便的. 直接tojson就好了.
格式化json直接fromJson. 这里要注意的是第二个参数要跟着你的内容走.实体类.class或是泛型集合.
FastJson
public static void fastJson() {
System.out.println("\n\nFastJson的使用: ");
Person person = new Person();
person.setId(1);
person.setName("Roy");
person.setPassword("Roy123");
List<Person> persons = new ArrayList();
Person person1 = null;
for(int i = 0; i<2;i++) {
person1 = new Person();
person1.setId(i);
person1.setName("Andy"+i);
person1.setPassword("Andy123"+i);
persons.add(person1);
}
System.out.println("对象转json: "+JSON.toJSONString(person));
System.out.println("集合转json: "+JSON.toJSONString(persons));
System.out.println("----------------------------------");
System.out.println("格式化jsonObject: "+JSON.parseObject(JSON.toJSONString(person),Person.class));
System.out.println("格式化jsonListObject: "+JSON.parseArray(JSON.toJSONString(persons), Person.class));
}
使用fastjson转json ,不管是对象还是集合都用JSON.toJSONString
但是需要注意的是.格式化json时,是有parseObject和parseArray的区分的.
只是在方法上的区分.第二个参数还是实体类.class
控制台打印信息
Jackson的使用:
对象转json: {"id":1,"name":"Roy","password":"Roy123"}
集合转json: [{"id":0,"name":"Andy0","password":"Andy1230"},{"id":1,"name":"Andy1","password":"Andy1231"}]
----------------------------------
格式化jsonObject: Person [id=1, name=Roy, password=Roy123]
格式化jsonListObject: [Person [id=0, name=Andy0, password=Andy1230], Person [id=1, name=Andy1, password=Andy1231]]
Gson的使用:
对象转json: {"id":1,"name":"Roy","password":"Roy123"}
集合转json: [{"id":0,"name":"Andy0","password":"Andy1230"},{"id":1,"name":"Andy1","password":"Andy1231"}]
----------------------------------
格式化jsonObject: Person [id=1, name=Roy, password=Roy123]
格式化jsonListObject: [Person [id=0, name=Andy0, password=Andy1230], Person [id=1, name=Andy1, password=Andy1231]]
FastJson的使用:
对象转json: {"id":1,"name":"Roy","password":"Roy123"}
集合转json: [{"id":0,"name":"Andy0","password":"Andy1230"},{"id":1,"name":"Andy1","password":"Andy1231"}]
----------------------------------
格式化jsonObject: Person [id=1, name=Roy, password=Roy123]
格式化jsonListObject: [Person [id=0, name=Andy0, password=Andy1230], Person [id=1, name=Andy1, password=Andy1231]]