如何解析json数据并将其加载到Listview中

时间:2018-01-12 06:36:54

标签: android json listview android-volley

我从api获取一个json对象,我想解析这个json对象并获取文本值。我正在使用volley库,需要在listiew适配器中加载它.json是对象数组对象类型。这是我的Json对象。你能帮忙吗?

{"result":[{"id":"1","ExpendName":"rice","Cost":"500","Dates":"2018-01-09 11:13:58"},{"id":"2","ExpendName":"Meat","Cost":"550","Dates":"2018-01-09 11:17:27"},{"id":"3","ExpendName":"Fish","Cost":"250","Dates":"2018-01-09 11:21:30"},{"id":"4","ExpendName":"Pant","Cost":"700","Dates":"2018-01-09 11:36:45"},{"id":"5","ExpendName":"Shirt","Cost":"1000","Dates":"2018-01-09 11:50:11"},{"id":"6","ExpendName":"Tea","Cost":"55","Dates":"2018-01-09 13:37:42"},{"id":"7","ExpendName":"Lunch, Tea, Transport ","Cost":"750","Dates":"2018-01-09 13:41:29"},{"id":"8","ExpendName":"Breakfast ","Cost":"30","Dates":"2018-01-10 05:34:07"},{"id":"9","ExpendName":"Train","Cost":"460","Dates":"2018-01-11 05:32:42"},{"id":"10","ExpendName":"Bus","Cost":"1250","Dates":"2018-01-11 05:33:11"},{"id":"11","ExpendName":"Train","Cost":"500","Dates":"2018-01-11 10:03:00"}]}

1 个答案:

答案 0 :(得分:0)

您通常会以这种方式解析数据:

try {
    JSONObject jsonObject = new JSONObject(rawData);
    JSONArray jsonArray = jsonObject.getJSONArray("result");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject mealObject = jsonArray.getJSONObject(i);
        String id = mealObject.getString("id");
        String expendName = mealObject.getString("ExpendName");
        String cost = mealObject.getString("Cost");
        String date = mealObject.getString("Dates");

        //Do whatever you want with the data
     }
 } catch (JSONException e) {
    e.printStackTrace();
 }