如何在Java中将String JSON格式拆分为对象的构造函数?

时间:2018-08-02 05:36:56

标签: java json constructor

我有一个Java类Payment.java,该类使用5个构造函数和所需的setter / getter构造“ Payment”对象,如下所示:

    public class Payment {

        //Contractors
        private Double amount, fee;
        private String TXID;
        private int time, type;

        public Payment(Double amount, Double fee, String TXID, int time, int type) {
            this.amount = amount;
            this.fee = fee;
            this.TXID = TXID;
            this.time = time;
            this.type = type;
        }

        public Double getAmount() {
            return amount;
        }

        public void setAmount(Double value) {
            this.amount = value;
        }

        public Double getFee() {
            return fee;
        }

        public void setFee(Double value) {
            this.fee = value;
        }

        public String getTXID() {
            return TXID;
        }

        public void setTXID(String value) {
            this.TXID = value;
        }


        public int getTime() {
            return time;
        }

        public void setTime(int value) {
            this.time = value;
        }

        public int getType() {
            return type;
        }

        public void setType(int value) {
            this.type = value;
        }

    }

我还有另一种方法,它向外部API发送一个HTTP(GET)请求,并以JSON格式作为字符串返回结果,如下所示:

{
  "result" : {
    "nh_wallet" : true,
    "payments" : [
      {
        "amount" : "0.00322253",
        "fee" : "0.00006577",
        "TXID" : "",
        "time" : 1533114558,
        "type" : 1
      },
      {
        "amount" : "0.00273396",
        "fee" : "0.0000558",
        "TXID" : "",
        "time" : 1533029140,
        "type" : 1
      },
      {
        "amount" : "0.00284428",
        "fee" : "0.00005805",
        "TXID" : "",
        "time" : 1532944513,
        "type" : 1
      },
      {
        "amount" : "0.00213709",
        "fee" : "0.00004361",
        "TXID" : "",
        "time" : 1532856970,
        "type" : 1
      }
    ],
    "base64MarchantID" : "366GT4q6hw7cVN6zzkFWfqZeKHHXZCp4rA"
  },
  "method" : "stats.provider.payments"
}

假设JSON结果字符串实际上是一个动态<List>Payment,那么最好的方法是将该字符串动态拆分为多个拆分的“付款”?

0 个答案:

没有答案