当前位置:网站首页>取Json中的数组进行遍历

取Json中的数组进行遍历

2022-08-10 16:31:00 辰远YIL

废话不多说,直接上代码

 测试:

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

原网站

版权声明
本文为[辰远YIL]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_55917018/article/details/126126967