当前位置:   article > 正文

String转换为JSON的几种方式_string to json

string to json

String转换为JSON的几种方式

common

采用springboot创建案例

1.导入依赖

<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.0</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

2.添加通用实体类


@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private int id;
    private String username;
    private String password;
}


@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserGroup {

    private String name;
    private List<User> users = new ArrayList<>();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

一、fastjson

 @Test
    public void test1(){
        //简单对象转换
        User user = new User(1,"root", "123456");
        System.out.println("初始化类:" + user);
        //调用toJSONString()
        String userJson = JSON.toJSONString(user);
        System.out.println("java类转换为json串:" + userJson);//{"id":1,"password":"123456","username":"root"}
        JSONObject jsonObject = JSONObject.parseObject(userJson);
        Integer id = jsonObject.getInteger("id");
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        System.out.println(id +"    :    "+username + "   :    " + password);
    }
    @Test
    public void test2(){
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);
        //调用toJSONString()
        String userJson = JSON.toJSONString(users);
        System.out.println("集合转json串:" + userJson);
        JSONArray jsonArray = JSONArray.parseArray(userJson);
        for (int i = 0; i < jsonArray.size(); i++) {
            Object obj = jsonArray.get(i);
            System.out.println(obj);// {"password":"111111","id":1,"username":"zhangsan"}
            String userJson1 = JSON.toJSONString(obj);
            JSONObject jsonObject = JSONObject.parseObject(userJson1);
            Integer id = jsonObject.getInteger("id");
            String username = jsonObject.getString("username");
            String password = jsonObject.getString("password");
            System.out.println(id +"    :    "+username + "   :    " + password);
        }

    }
    @Test
    public void test3(){
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);
        //复杂java类转换对象
        UserGroup userGroup = new UserGroup("userGroup", users);
        System.out.println("初始化类:" + userGroup);
        //调用toJSONString()
        String userGroupJson = JSON.toJSONString(userGroup);
        System.out.println("复杂java类转换json串:" + userGroupJson);

        JSONObject jsonObjects = JSONObject.parseObject(userGroupJson);
        String name = jsonObjects.getString("name");
        String usersArray = jsonObjects.getString("users");
        System.out.println(name);
        System.out.println(usersArray);

        JSONArray jsonArray = JSONArray.parseArray(usersArray);
        for (int i = 0; i < jsonArray.size(); i++) {
            Object obj = jsonArray.get(i);
            System.out.println(obj);// {"password":"111111","id":1,"username":"zhangsan"}
            String userJson1 = JSON.toJSONString(obj);
            JSONObject jsonObject = JSONObject.parseObject(userJson1);
            Integer id = jsonObject.getInteger("id");
            String username = jsonObject.getString("username");
            String password = jsonObject.getString("password");
            System.out.println(id +"    :    "+username + "   :    " + password);
        }
    }
    
PS:代码繁琐一点,但结果获取到了!!!    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

二、gson

@Test
    public void test1(){
        //简单对象转换
        User user = new User(1,"root", "123456");
        System.out.println("初始化类:" + user);
        //创建gson对象
        Gson gson = new Gson();
        // 调用Gson的String toJson(Object)方法将Bean转换为json字符串
        String jsonStr = gson.toJson(user);
        System.out.println("java类转换为json串:" + jsonStr);
        User fromJson = gson.fromJson(jsonStr, User.class);
        int id = fromJson.getId();
        String username = fromJson.getUsername();
        String password = fromJson.getPassword();
        System.out.println(id +"    :    "+username + "   :    " + password);
    }
    @Test
    public void test2(){
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);
        //创建gson对象
        Gson gson = new Gson();
        // 调用Gson的String toJson(Object)方法将Bean转换为json字符串
        String jsonStr = gson.toJson(users);
        System.out.println("java类转换为json串:" + jsonStr);
        List list = gson.fromJson(jsonStr, List.class);
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            String json = gson.toJson(obj);
            User user = gson.fromJson(json, User.class);
            int id = user.getId();
            String username = user.getUsername();
            String password = user.getPassword();
            System.out.println(id +"    :    "+username + "   :    " + password);
        }
    }
    @Test
    public void test3(){
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);

        //复杂java类转换对象
        UserGroup userGroup = new UserGroup("userGroup", users);
        System.out.println("初始化类:" + userGroup);
        //创建gson对象
        Gson gson = new Gson();
        // 调用Gson的String toJson(Object)方法将Bean转换为json字符串
        String userGroupJson = gson.toJson(userGroup);
        System.out.println("复杂java类转换json串:" + userGroupJson);
        UserGroup userGroupStr = gson.fromJson(userGroupJson, UserGroup.class);
        String userGroupStrName = userGroupStr.getName();
        List<User> usersList = userGroupStr.getUsers();
        System.out.println(userGroupStrName);
        System.out.println(usersList);
        for (int i = 0; i < usersList.size(); i++) {
            Object obj = usersList.get(i);
            String json = gson.toJson(obj);
            User user = gson.fromJson(json, User.class);
            int id = user.getId();
            String username = user.getUsername();
            String password = user.getPassword();
            System.out.println(id +"    :    "+username + "   :    " + password);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75

三、Jackson

 @Test
    public void test1() throws JsonProcessingException {
        //简单对象转换
        User user = new User(1,"root", "123456");
        System.out.println("初始化类:" + user);
        //创建jackson对象
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(user);
        System.out.println("java类转换为json串:" + jsonStr);
        User fromJson = objectMapper.readValue(jsonStr, User.class);
        int id = fromJson.getId();
        String username = fromJson.getUsername();
        String password = fromJson.getPassword();
        System.out.println(id +"    :    "+username + "   :    " + password);
    }
    @Test
    public void test2() throws JsonProcessingException {
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);
        //创建jackson对象
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(users);
        System.out.println("java类转换为json串:" + jsonStr);
        List list = objectMapper.readValue(jsonStr, List.class);

        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            String jsonStrs = objectMapper.writeValueAsString(obj);
            User user = objectMapper.readValue(jsonStrs, User.class);
            int id = user.getId();
            String username = user.getUsername();
            String password = user.getPassword();
            System.out.println(id +"    :    "+username + "   :    " + password);
        }
    }
    @Test
    public void test3() throws JsonProcessingException {
        //集合转json串
        User user1 = new User(1,"zhangsan", "111111");
        User user2 = new User(2,"lisi", "222222");
        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        System.out.println("初始化类:" + users);

        //复杂java类转换对象
        UserGroup userGroup = new UserGroup("userGroup", users);
        System.out.println("初始化类:" + userGroup);
        //创建jackson对象
        ObjectMapper objectMapper = new ObjectMapper();
        String userGroupJson = objectMapper.writeValueAsString(userGroup);
        System.out.println("复杂java类转换json串:" + userGroupJson);
        UserGroup userGroupStr = objectMapper.readValue(userGroupJson, UserGroup.class);
        String userGroupStrName = userGroupStr.getName();
        List<User> usersList = userGroupStr.getUsers();
        System.out.println(userGroupStrName);
        System.out.println(usersList);
        for (int i = 0; i < usersList.size(); i++) {
            Object obj = usersList.get(i);
            String jsonStrs = objectMapper.writeValueAsString(obj);
            User user = objectMapper.readValue(jsonStrs, User.class);
            int id = user.getId();
            String username = user.getUsername();
            String password = user.getPassword();
            System.out.println(id +"    :    "+username + "   :    " + password);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/1014131
推荐阅读
相关标签
  

闽ICP备14008679号