首页 前端知识 String转换为JSON的几种方式

String转换为JSON的几种方式

2024-05-31 19:05:13 前端知识 前端哥 792 833 我要收藏

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>

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<>();
}

一、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:代码繁琐一点,但结果获取到了!!!    

二、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);
        }
    }

三、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);
        }
    }
转载请注明出处或者链接地址:https://www.qianduange.cn//article/10227.html
标签
junit
评论
发布的文章

JSON&yaml和Properties

2024-06-06 10:06:54

JavaScript中的JSON.stringify()

2024-06-06 10:06:52

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!