gpt4 book ai didi

java - 使用java创建json对象

转载 作者:行者123 更新时间:2023-12-01 12:27:11 24 4
gpt4 key购买 nike

我尝试了多种方法来使用 java 创建以下格式的 json。 (Gson, jackson ,...)

{
"outbound": {
"address": [
"t91",
"t0992"
],
"send": "t678",
"outMessage": {
"message": "Hello World"
},
"client": "156",
"receipt": {
"URL": "http://example.com/Delivery",
"callback": "some-to-the-request"
},
"senderName": "Inc."
}
}

有什么帮助吗?

最佳答案

使用下面的代码。创建 POJO

public class TestPojo {
private Outbound outbound;

public Outbound getOutbound() {
return outbound;
}

public void setOutbound(Outbound outbound) {
this.outbound = outbound;
}

}

class Outbound {
private String[] address;
private String send;
private OutMessage outMessage;
private Receipt receipt;
private String senderName;

public String[] getAddress() {
return address;
}

public void setAddress(String[] address) {
this.address = address;
}

public String getSend() {
return send;
}

public void setSend(String send) {
this.send = send;
}

public OutMessage getOutMessage() {
return outMessage;
}

public void setOutMessage(OutMessage outMessage) {
this.outMessage = outMessage;
}

public Receipt getReceipt() {
return receipt;
}

public void setReceipt(Receipt receipt) {
this.receipt = receipt;
}

public String getSenderName() {
return senderName;
}

public void setSenderName(String senderName) {
this.senderName = senderName;
}

}

class OutMessage {
private String message;

public OutMessage(String message) {
super();
this.message = message;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

class Receipt {
private String URL;
private String callback;

public Receipt(String uRL, String callback) {
super();
URL = uRL;
this.callback = callback;
}

public String getURL() {
return URL;
}

public void setURL(String uRL) {
URL = uRL;
}

public String getCallback() {
return callback;
}

public void setCallback(String callback) {
this.callback = callback;
}

}

主类(JSON 到对象)

String json = "{'outbound':{'address':['t91','t0992'],'send':'t678','outMessage':{'message':'Hello World'},'receipt':{'URL':'http://example.com/Delivery','callback':'some-to-the-request'},'senderName':'Inc.'}}";
TestPojo testPojo = new Gson().fromJson(json, TestPojo.class);
System.out.println(testPojo.getOutbound().getSenderName());

输出

Inc.

主类(对象到 JSON)

TestPojo testPojo = new TestPojo();
Outbound outbound = new Outbound();

outbound.setAddress(new String[]{"t91", "t0992"});
outbound.setOutMessage(new OutMessage("Hello World"));
outbound.setReceipt(new Receipt("http://example.com/Delivery", "some-to-the-request"));
outbound.setSenderName("Inc.");
outbound.setSend("t678");
testPojo.setOutbound(outbound);

System.out.println(new Gson().toJson(testPojo));

输出

{"outbound":{"address":["t91","t0992"],"send":"t678","outMessage":{"message":"Hello World"},"receipt":{"URL":"http://example.com/Delivery","callback":"some-to-the-request"},"senderName":"Inc."}}

详情

使用了GSON库。使用了你给的json。

关于java - 使用java创建json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26234018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com