使用Java创建JSON对象

Manitaz

我尝试了几种使用java创建跟随格式json的方法。(格森,杰克逊,...)

{
  "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."
  }
}

有什么帮助吗?

Yogesh prajapati

使用下面的代码。创建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。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章