JSON

java Json 技术记录(3)

字号+ 作者:H5之家 来源:H5之家 2017-12-13 17:11 我要评论( )

package com.jxufe.study.jsonstudy.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; import com.jxufe.study.j

package com.jxufe.study.jsonstudy.test; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; import com.jxufe.study.jsonstudy.model.User; import org.junit.Test; import java.util.*; /** * @author hong.zheng * @Description * @date 2017-12-07-16:14 * FastJsonTest { @Test public void beanToJson() { System.out.println(JSON.toJSON(new User("mindong",11,new Date(),"123"))); System.out.println(JSON.toJSON("zhenghong")); } @Test public void jsonToBean() { String jsonStr = "{\"birthday\":1512634693590,\"name\":\"mindong\",\"age\":11,\"email\":\"123\",\"testName\":\"Today is 2017-12-07\"}\n"; System.out.println(JSON.parseObject(jsonStr,User.class)); } @Test public void testJsonToList() { List<User> usersList = new ArrayList<User>(); usersList.add(new User("zengting",23,new Date(),"[email protected]")); usersList.add(new User("mindong",23,new Date(),"[email protected]")); usersList.add(new User("liliang",21,new Date(),"[email protected]")); JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd"; String jsonList = JSON.toJSONString(usersList, SerializerFeature.WriteDateUseDateFormat); System.out.println(jsonList); System.out.println("------------------------------------------------------------------"); List<User> list = JSON.parseArray(jsonList,User.class); System.out.println(list); System.out.println("------------------------------------------------------------------"); System.out.println( JSON.parseObject(jsonList,new TypeReference<List<User>>(){})); } @Test public void testMapToJson() { Map<Integer,User> userMap = new HashMap<Integer, User>(); userMap.put(1,new User("zengting",23,new Date(),"[email protected]")); userMap.put(2,new User("mindong",22,new Date(),"[email protected]")); userMap.put(3,new User("liliang",25,new Date(),"[email protected]")); String jsonMap = JSON.toJSONString(userMap); System.out.println(jsonMap); Map<Integer,User> fromJsonMap = JSON.parseObject(jsonMap,new TypeReference<Map<Integer, User>>(){}); System.out.println(fromJsonMap); } }

4.jackson-lib 使用

package com.jxufe.study.jsonstudy.test;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jxufe.study.jsonstudy.model.User;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author hong.zheng
* @Description
* @date 2017-12-08-9:29
**/
public class JacksonTest {
@Test
public void beanToJson() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
//writeObject可以转换java对象,eg:JavaBean/Map/List/Array等
User user = new User("zengting",23,new Date(),"[email protected]");
String jsonString = objectMapper.writeValueAsString(user);
System.out.println(jsonString);
}
@Test
public void jsonToBean () throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String jsonString = "{\"name\":\"zengting\",\"age\":23,\"birthday\":1512696816772,\"email\":\"[email protected]\",\"1TestName\":\"Today is 2017-12-07\"}\n";
User user = objectMapper.readValue(jsonString, User.class);
System.out.println(user);
}
@Test
public void listToJson() throws IOException {
List<User> list = new ArrayList<User>();
list.add(new User("mindong",31,new Date(),"1321"));
list.add(new User("zengting",31,new Date(),"1321"));
list.add(new User("liliang",31,new Date(),"1321"));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(System.out,list);

}
@Test
public void jsonTolist() throws IOException {
String json = "[{\"name\":\"mindong\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"zengting\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"liliang\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List<User> list = objectMapper.readValue(json ,List.class);
for (int i = 0; i <list.size() ; i++) {
User user = list.get(i); // 转化失败,List<User> 的值是List<LinkedHashMap<String,Object>>,LinkedHashMap<String,Object>不能转化为User
System.out.println(list.get(i));
}
}
@Test
public void jsonTolist1() throws IOException {
String json = "[{\"name\":\"mindong\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"zengting\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"liliang\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String,Object>> list = objectMapper.readValue(json ,List.class);
for (int i = 0; i <list.size() ; i++) {
Map<String,Object> map = list.get(i);
String name = map.get("name").toString();
Integer age = Integer.valueOf(map.get("age").toString());
Date date = new Date(Long.valueOf(map.get("birthday").toString()));
String email = map.get("email").toString();
User user = new User(name,age,date,email);
System.out.println(user);
}
}
@Test
public void jsonToArray() throws IOException {
String json = "[{\"name\":\"mindong\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"zengting\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"},{\"name\":\"liliang\",\"age\":31,\"birthday\":1512698926871,\"email\":\"1321\",\"1TestName\":\"Today is 2017-12-07\"}]";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

User []users = objectMapper.readValue(json ,User[].class);
for (int i = 0; i <users.length ; i++) {
User user = users[i];
System.out.println(user.getClass());
System.out.println(users[i]);
}
}

}

四种解析方式对Date类型的解析不太一样

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • ASP.NET Core教程【一】关于Razor Page的知识

    ASP.NET Core教程【一】关于Razor Page的知识

    2017-11-30 18:05

  • python学习之路(二)

    python学习之路(二)

    2017-11-24 11:25

  • sqlserverToXML

    sqlserverToXML

    2017-11-10 17:05

  • shelve模块

    shelve模块

    2017-11-06 13:06

网友点评