首页 > 基础资料 博客日记

常用的将Java的String字符串转具体对象的几种方式

2024-09-08 21:00:07基础资料围观150

本篇文章分享常用的将Java的String字符串转具体对象的几种方式,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识

Java对象以User.class为例,注意:代码中使用到了lombok的@Data注解

package com.example.entity;

import lombok.Data;

@Data
public class User {

    // 姓名
    private String name;

    // 年龄
    private int age;
}

1.Jsonlib解析器

依赖

		<!--json-lib-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

字符串转对象-代码展示

package com.example.demo;

import com.example.entity.User;
import net.sf.json.JSONObject;

public class TestA {

    public static void main(String[] args) throws Exception {
        String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
        //1、使用JSONObject
        JSONObject jsonObject = JSONObject.fromObject(objectStr);
        User user = (User) JSONObject.toBean(jsonObject, User.class);
        System.out.println("name:" + user.getName());
        System.out.println("age:" + user.getAge());
    }

}

打印截图示例

延伸(对象转String)代码展示

package com.example.demo;

import com.example.entity.User;
import net.sf.json.JSONObject;

public class TestA {

    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("张三");
        user.setAge(18);

        // 1.对象转JSONObject
        JSONObject json = JSONObject.fromObject(user);
        // 2.JSONObject转String
        String strJson = json.toString();
        System.out.println(strJson);
    }

}

延伸(对象转String)运行截图

2.fastjson解析器

依赖

		<!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

字符串转对象-代码展示

package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.entity.User;

public class TestA {

    public static void main(String[] args) throws Exception {
        String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
        JSONObject jsonObject = JSON.parseObject(objectStr);
        User user = JSON.toJavaObject(jsonObject, User.class);
        System.out.println("name:" + user.getName());
        System.out.println("age:" + user.getAge());

    }

}

打印截图示例

延伸(对象转String)代码展示

package com.example.demo;

import com.alibaba.fastjson.JSON;
import com.example.entity.User;

public class TestA {

    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("张三");
        user.setAge(18);

        String str = JSON.toJSONString(user);
        System.out.println(str);
    }

}

延伸(对象转String)运行截图

3.jackson解析器

依赖

		<!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.7.1</version>
        </dependency>

字符串转对象-代码展示

package com.example.demo;

import com.example.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestA {

    public static void main(String[] args) throws Exception {
        String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
        ObjectMapper om = new ObjectMapper();
        User user = om.readValue(objectStr, User.class);
        System.out.println("name:" + user.getName());
        System.out.println("age:" + user.getAge());
    }

}

打印截图示例

延伸(对象转String)代码展示

package com.example.demo;

import com.example.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestA {

    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("张三");
        user.setAge(18);

        ObjectMapper om = new ObjectMapper();
        String str = om.writeValueAsString(user);
        System.out.println(str);
    }

}

延伸(对象转String)运行截图

4.Gson解析器

依赖

<!-- 谷歌依赖 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

字符串转对象-代码展示

package com.example.demo;

import com.example.entity.User;
import com.google.gson.Gson;

public class TestA {

    public static void main(String[] args) throws Exception {
        String objectStr = "{\"name\":\"张三\",\"age\":\"18\"}";
        Gson gson = new Gson();
        User user = gson.fromJson(objectStr, User.class);
        System.out.println("name:" + user.getName());
        System.out.println("age:" + user.getAge());
    }

}

打印截图示例

延伸(对象转String)代码展示

package com.example.demo;

import com.example.entity.User;
import com.google.gson.Gson;

public class TestA {

    public static void main(String[] args) throws Exception {
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        Gson gson = new Gson();

        String str = gson.toJson(user);
        System.out.println(str);
    }

}

延伸(对象转String)运行截图


以上就是常用的几种String转具体的java对象操作


文章来源:https://blog.csdn.net/qq_42991839/article/details/135629715
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云