如何将java对象转换为JSON格式而不使用任何外部库或java依赖项?

yrdbyhpb  于 2023-06-25  发布在  Java
关注(0)|答案(2)|浏览(226)
static class DataObject {
    private String access;
    private String foo;
    private Int age;
}

{"access" : "value","foo":"value","age":"value"}
而不用格森或Jackson只是香草Java
有更简单的方法吗?

csga3l58

csga3l581#

在Java 20中,JRE中仍然没有包含JSON API。
因此,您的答案是自己编写String,例如使用String.format
关于类结构,可以使用records

vwhgwdsa

vwhgwdsa2#

要动态地做到这一点,很可能需要递归,我猜还需要反射。
对于静态内容,一个简单的实现是使用 toString 方法。
下面是一个例子,给出了您在评论中提供的数据。
这里的 Formatter 类只是 String#format 方法的扩展。
特别是,它与 JSON 解析没有相关性。

class DataObject {
    String aPid;
    String location;
    String senderDomainId;
    String timeZone;
    String senderDomainAgentId;
    String recipientDomainName;
    BusinessMsg businessMsg;

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        Formatter formatter = new Formatter(string);
        formatter.format("{%n");
        formatter.format("\"sendParams\": {".indent(4), aPid);
        formatter.format("\"aPid\": \"%s\",".indent(8), aPid);
        formatter.format("\"location\": \"%s\",".indent(8), location);
        formatter.format("\"senderDomainId\": \"%s\",".indent(8), senderDomainId);
        formatter.format("\"timeZone\": \"%s\",".indent(8), timeZone);
        formatter.format("\"senderDomainAgentId\": \"%s\",".indent(8), senderDomainAgentId);
        formatter.format("\"recipientDomainName\": \"%s\",".indent(8), recipientDomainName);
        formatter.format("\"businessMsg\": %s".indent(8), businessMsg);
        formatter.format("}".indent(4));
        formatter.format("}");
        formatter.flush();
        return string.toString();
    }

    static class BusinessMsg {
        String id, to, from;

        @Override
        public String toString() {
            StringBuilder string = new StringBuilder();
            Formatter formatter = new Formatter(string);
            formatter.format("{%n");
            formatter.format("\"id\": \"%s\",".indent(12), id);
            formatter.format("\"to\": \"%s\",".indent(12), to);
            formatter.format("\"from\": \"%s\"".indent(12), from);
            formatter.format("}".indent(8).stripTrailing());
            formatter.flush();
            return string.toString();
        }
    }
}

然后,如果我们填充数据,并使用 println 调用,我们将获得一个有效的 JSON 值。

DataObject dataObject = new DataObject();
dataObject.aPid = "AGNT8935i";
dataObject.location = "New York";
dataObject.senderDomainId = "DOMN67";
dataObject.timeZone = "Eastern Standard Time";
dataObject.senderDomainAgentId = "DMNAGNT94-eoi7436ur-334kjery3-oe73573ne";
dataObject.recipientDomainName = "tic.com";
DataObject.BusinessMsg businessMsg = new DataObject.BusinessMsg();
businessMsg.id = "BSMG227e0e9f-2af9-4f49-8886-39eadda4d8f3";
businessMsg.to = "tic.com";
businessMsg.from = "tic.com";
dataObject.businessMsg = businessMsg;

System.out.println(dataObject);

输出量

{
    "sendParams": {
        "aPid": "AGNT8935i",
        "location": "New York",
        "senderDomainId": "DOMN67",
        "timeZone": "Eastern Standard Time",
        "senderDomainAgentId": "DMNAGNT94-eoi7436ur-334kjery3-oe73573ne",
        "recipientDomainName": "tic.com",
        "businessMsg": {
            "id": "BSMG227e0e9f-2af9-4f49-8886-39eadda4d8f3",
            "to": "tic.com",
            "from": "tic.com"
        }
    }
}

相关问题