简体   繁体   中英

How to extract multiple values from json response in java

I would like to get multiple values from json response. Following is the response returned by my code.

{
  "data": [
    {
      "boxnumber": "1",
      "quantity": 1,
      "product": "7010501264",
      "packdate": "2014-10-16 20:59:25.632533"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1614120",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1803068",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1806139",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1806140",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1799753",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1806138",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1801962",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "8225611",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "155611",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "8225632",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1799595",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1791274",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1802853",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1614146",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1614127",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1801911",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1829209",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1802902",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1614110",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1614112",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "1",
      "quantity": 1,
      "product": "7010500140",
      "packdate": "2014-10-16 20:59:25.632533"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1799719",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1799594",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "1",
      "quantity": 2,
      "product": "7010100150",
      "packdate": "2014-10-16 20:59:25.632533"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1806151",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "8225630",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1806137",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1791277",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "1",
      "quantity": 1,
      "product": "8010120130",
      "packdate": "2014-10-16 20:59:25.632533"
    },
    {
      "boxnumber": "1",
      "quantity": 1,
      "product": "8010120160",
      "packdate": "2014-10-16 20:59:25.632533"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1801913",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "8225612",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1801919",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1614125",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1614111",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1802861",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1799752",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1043",
      "quantity": 1,
      "product": "1799596",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1614113",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1799748",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "1799597",
      "packdate": "2014-10-16 12:53:33.663165"
    },
    {
      "boxnumber": "13-ARTENGO-P1046",
      "quantity": 1,
      "product": "8225610",
      "packdate": "2014-10-16 12:53:33.663165"
    }
  ],
  "status": "success"
}

Please any one help me to parse and assign into four variables

You can use the gson library.

public class Main {

public static void main(String... args) throws Exception {
    String json = 
        "{"
            + "'status' : 'success',"
            + "'data' : [{"
                + "'boxnumber' : '1',"
                + "'quantity' : 1,"
                + "'product' : '7010501264',"
                + "'packdate' : '2014-10-16 20:59:25.632533'"
                + "}]" 
        + "}";

    // Now do the magic.
    Gson gson = new GsonBuilder().create();
    YourObject data = gson.fromJson(json, YourObject.class);

    // Show it.
    System.out.println(data);
}

}
class YourObject {
    private String status;
    private List<Data> data;

    // getter and setter !!

    @Override
    public String toString() {
        return "YourObject [status=" + status + ", data=" + data + "]";
    }

}
class Data {
    private String boxnumber;
    private int quantity;
    private long product;
    private String packdate;

    // getter and setter

    @Override
    public String toString() {
        return "Data [boxnumber=" + boxnumber + ", quantity=" + quantity
                + ", product=" + product + ", packdate=" + packdate + "]";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM