0

I have a java class Payment.java which constructs the "Payment" object with 5 constructors and required setter/getters as following:

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

    }

I have another method which sends a HTTP(GET) request to an external API and returns the results in JSON format as a String as shown below:

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

Supposing that the JSON result String is actually a dynamic <List>Payment, what is the best way to split this String into several splitted "Payment"(s) dynamically?

Saffa Seraj
  • 77
  • 1
  • 8
  • 1
    just serialise it into a List it would fit in correctly, which serialisation library are you using? – oziomajnr Aug 02 '18 at 05:39
  • error in your json response –  Aug 02 '18 at 05:40
  • Error: Parse error on line 1: "payments": [{ "amount": "0. ----------^ Expecting 'EOF', '}', ',', ']', got ':' –  Aug 02 '18 at 05:40
  • 1
    your response json is not in proper manner view –  Aug 02 '18 at 05:42
  • @Ram Mohan dubey : You're right I fixed it @ Ogbe : java.util.ArrayList & java.util.List I'm not sure if I answered your question. However, which library do you recommend? – Saffa Seraj Aug 02 '18 at 05:49
  • *** AN IMPORTANT REMINDER *** Obviously the String is containing ALL the JSON format result; but I want only exploit the json fields which matches with constructors (amount, fee, TXID, time, type) that would be another challenge! – Saffa Seraj Aug 02 '18 at 05:52
  • I mostly use retrofit and Asyncktask sometimes –  Aug 02 '18 at 06:33

0 Answers0