zl程序教程

您现在的位置是:首页 >  后端

当前栏目

取Json中的数组进行遍历

2023-06-13 09:15:10 时间

废话不多说,直接上代码

 测试:

String test = "{"list":[{"id":1,"qty":20,"type":"测试","time":"2022-08-02"},{"id":2,"qty":10,"type":"测试","time":"2022-08-02"},{"id":3,"qty":17,"type":"测试","time":"2022-08-02"}]}"
JSONObject obj = JSONObject.fromObject(test);
Object object = obj.get("list");
JSONArray jsonObject = JSONArray.fromObject(object);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  //日期格式化
for(int i = 0;i < jsonObject.size(); i++){
    JSONObject onecar = jsonObject.getJSONObject(i);
    int uid = onecar.getInt("id");
    int qty = onecar.getInt("qty");
    String type = onecar.getString("type");
    String time = onecar.getString("time");
    try {
			times = sdf.parse(time);  //日期需要try一下不然会异常
		} catch (ParseException e) {
			e.printStackTrace();
		}
}