如何从java中的json响应中提取多个值

时间:2014-11-09 09:19:29

标签: java json parsing

我想从json响应中获取多个值。以下是我的代码返回的响应。

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

请任何人帮我解析并分配四个变量

1 个答案:

答案 0 :(得分:0)

您可以使用gson库。

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 + "]";
    }