首页 > 解决方案 > 如何将字符串 JSON 格式拆分为 Java 中对象的构造函数?

问题描述

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

    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;
        }

    }

HTTP(GET)我有另一种方法,它向外部 API发送请求并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的,将这个字符串动态拆分为多个拆分的“付款”的最佳方法是什么?

标签: javajsonconstructor

解决方案


推荐阅读