常见的转换工具有:
Jackson:SpringMVC内置的转换工具
jsonlib:Java提供的转换工具(一般不用)
gson:google提供的转换工具(轻量级的框架)
fastjson:Alibaba提供的转换工具(效率高速度快)
Jackson:
相关jar包:
1 2 3 |
jackson-annotations-2.2.3.jar jackson-core.2.2.3.jar jackson-databind-2.2.3.jar |
主要方法:
1 2 3 4 5 6 7 |
//对象----->字符串 String jsonStr = new ObjectMapper().**writeValueAsString**(resultBean); //字符串--->map Map map = JSON.**parseObject**(jsonStr, Map.class); //字符串---> list TypeReference<List<User>> ref = new TypeReference<List<User>>(){}; List<User> list = objectMapper.readValue(jsonStr, ref); |
java对象转json:
1 2 3 4 5 6 7 8 9 10 11 12 |
//Map对象或者JavaBean对象转换成json的时候会得到一个json字符串 //List<Map>或者List<JavaBean>转换成json的时候会得到一个json数组的字符串 @Test public void test04() throws JsonProcessingException { List<User> userList = new ArrayList<>(); userList.add(new User(1, "娜扎", "123456", "18999999999@163.com", "18999999999")); userList.add(new User(2, "热巴", "123456", "18999999999@163.com", "19898989898")); userList.add(new User(3, "哈尼", "123456", "18999999999@163.com", "19866666666")); ResultBean resultBean = new ResultBean(true, userList); String jsonStr = new ObjectMapper().writeValueAsString(resultBean); System.out.println(jsonStr); } |
json字符串转对象:
使用jackson将json字符串转换JavaBean对象或者Map
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test //把json转成JavaBean(user对象) public void test06() throws IOException { String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"}"; //1.调用JSON.parseObject(String json,Class clazz); //转换成user User user = JSON.parseObject(jsonStr, User.class); System.out.println(user); //转换成map Map map = JSON.parseObject(jsonStr, Map.class); System.out.println(map); } |
使用jackson将json数组字符串转换成List
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test //把json转成List<JavaBean>对象 public void test07() throws Exception { String jsonStr = "[{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"},{\"id\":2,\"username\":\"ls\",\"password\":\"123456\",\"email\":\"ls@163.com\",\"phone\":\"1386781898\"},{\"id\":3,\"username\":\"ww\",\"password\":\"123456\",\"email\":\"ww@163.com\",\"phone\":\"1386782898\"}]"; //1.创建ObjectMapper对象 ObjectMapper objectMapper = new ObjectMapper(); //2.调用readValue() TypeReference<List<User>> ref = new TypeReference<List<User>>(){}; List<User> list = objectMapper.readValue(jsonStr, ref); System.out.println(list); } |
fastjson
jar包:fastjson-1.2.39.jar
主要方法:
1 2 3 |
String jsonStr = JSON.toJSONString(user) //user(对象) -----> jsonStr(字符串) Map map = JSON.parseObject(jsonStr, Map.class); //jsonStr(字符串) --> map List<User> userList = JSON.parseArray(jsonArr, User.class); //jsonStr(字符串) --> List |
使用fastjson将java对象转成json字符串
1 2 3 4 5 6 7 |
@Test public void test06(){ //使用fastjson将user对象转换成json字符串 User user = new User(1,"张三","123456","123456@qq.com","18999999999"); String jsonStr = JSON.toJSONString(user); System.out.println(jsonStr); } |
使用fastjson将json字符串转换成JavaBean对象或者Map
1 2 3 4 5 6 7 8 9 10 |
@Test //把json转成JavaBean(user对象) public void test08() throws IOException { String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"}"; //1.调用JSON.parseObject(String json,Class clazz); User user = JSON.parseObject(jsonStr, User.class); System.out.println(user); } |
1 2 3 4 5 6 7 8 |
@Test //把json转成Map public void test09() throws IOException { String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"}"; //1.调用JSON.parseObject(String json,Class clazz); Map map = JSON.parseObject(jsonStr, Map.class); System.out.println(map); } |
1 2 3 4 5 6 7 8 9 10 |
@Test //使用fastjson将json字符串转换成List<JavaBean> public void test11() { //使用fastjson将json数组的字符串,转换成List<User> String jsonArr = "[{\"id\":1,\"username\":\"张三\",\"password\":\"123456\",\"email\":\"123456@qq.com\",\"phone\":\"18999999999\"},{\"id\":2,\"username\":\"李四\",\"password\":\"654321\",\"email\":\"654321@qq.com\",\"phone\":\"18666666666\"},{\"id\":3,\"username\":\"王五\",\"password\":\"777777\",\"email\":\"777777@qq.com\",\"phone\":\"18777777777\"}]"; List<User> userList = JSON.parseArray(jsonArr, User.class); for (User user : userList) { System.out.println(user.getUsername()); } } |
fastjson解析复杂json数据:
1、如何从字符串String获得JSONObject对象和JSONArray对象
1 2 |
JSONObject jsonObject = new JSONObject ( String str); JSONArray jsonArray = new JSONArray(String str ) ; |
2、如何从JSONArray中获得JSONObject对象
可以把JSONArray当成一般的数组来对待,只是获取的数据内数据的方法不一样
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
3、获取JSON内的数据
1 2 |
int mid= jsonObject.getInt ( "id" ) ; String mcourse=jsonObject.getString( " courseID") ; |