0

Here is my JSON response

{
  "data": [
    {
      "id": "txn_17pHAkI4mX5ntfze4If5wIxW",
      "source": "tr_17pHAkI4mX5ntfzeIfNucubD",
      "amount": -100000,
      "currency": "usd",


    },
    {
      "id": "txn_17pH21I4mX5ntfzesrhdZwyf",
      "source": "tr_17pH21I4mX5ntfzeKLd0SWw0",
      "amount": -100000,

    },
    {
      "id": "txn_17pGVRI4mX5ntfzeBQPCWZZg",
      "source": "tr_17pGVRI4mX5ntfzegJNe1r4o",
      "amount": -100000,
      "currency": "usd"  
    }
  ],

  },
  "url": "/v1/balance/history",
  "count": null
}

I want to get "amount" and "id" from these three list. How to do it in java?

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Shifat
  • 732
  • 1
  • 6
  • 20
  • 2
    Possible duplicate of [How to parse json data](http://stackoverflow.com/questions/21824641/how-to-parse-json-data) – Armfoot Mar 15 '16 at 14:03
  • Basic question, already answered many times. Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Milan Mar 15 '16 at 14:16

1 Answers1

2

Look into Google's Gson.

You need to create a class that represents the json data.

class Response {
    class Data {
        public String id;
        public String source;
        public int amount;
        public String currency;
    }
    public Data[] data;
    public String url;
    public int count;
}

Then read the data into a variable.

String json = "your json here";
Gson gson = new Gson();
Response response = gson.fromJson(json, Response.class);
for(Data d : response.data) {
    System.out.println(d.id);
    System.out.println(d.amount);
}
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38