如何在android中解析包含对象内部对象并在listview中填充的json

时间:2015-07-29 18:53:39

标签: android json parsing

{
  "data": [
    {
      "0": {
        "degree": "HSC",
        "start_date": "01-01-2007",
        "end_date": "01-01-2008"
      },
      "1": {
        "degree": "BE",
        "start_date": "01-01-2008",
        "end_date": "31-05-2012"
      },
      "2": {
        "degree": "MCM",
        "start_date": "29-07-2015",
        "end_date": "31-07-2015"
      },
      "3": {
        "exp_designation": "aaa",
        "exp_description": "aa",
        "exp_startdate": "23-07-2015",
        "exp_enddate": "01-07-2015",
        "latest": "0"
      },
      "4": {
        "exp_designation": "aaa",
        "exp_description": "asasasas",
        "exp_startdate": "01-07-2015",
        "exp_enddate": "31-07-2015",
        "latest": "0"
      },
      "user_id": "1",
      "user fullname": "",
      "user_phone": "2147483647",
      "user_email": "",
      "user_address": "Uruli kanchan",
      "user_status": "",
      "user_speciality": "eye",
      "username": "",
      "password": "niki"
    }
  ]
}

如何在android中解析这个json?我想要显示

"0": {
        "degree": "HSC",
        "start_date": "01-01-2007",
        "end_date": "01-01-2008"
      },
      "1": {
        "degree": "BE",
        "start_date": "01-01-2008",
        "end_date": "31-05-2012"
      },
      "2": {
        "degree": "MCM",
        "start_date": "29-07-2015",
        "end_date": "31-07-2015"
      },
      "3": {
        "exp_designation": "aaa",
        "exp_description": "aa",
        "exp_startdate": "23-07-2015",
        "exp_enddate": "01-07-2015",
        "latest": "0"
      },
      "4": {
        "exp_designation": "aaa",
        "exp_description": "asasasas",
        "exp_startdate": "01-07-2015",
        "exp_enddate": "31-07-2015",
        "latest": "0"
      },

这些在自定义列表视图中?

这是一个正确的json吗?

如何解析?

目前我正在获取一些json数据,如下所示,

// Getting JSON Array
                user = json.getJSONArray("data");
                JSONObject feedObj = user.getJSONObject(0);
                item.setUserid(feedObj.getInt("user_id"));
                item.setUserFullname(feedObj.getString("user fullname"));

                item.setUserPhone(feedObj.getString("user_phone"));
                MobileNo.setText(item.getUserPhone());

                item.setUserEmail(feedObj.getString("user_email"));
                Email.setText(item.getUserEmail());

                item.setUserAddress(feedObj.getString("user_address"));
                Address.setText(item.getUserAddress());

                item.setUserStatus(feedObj.getString("user_status"));

                item.setUserSpeciality(feedObj.getString("user_speciality"));
                SelSpeciality.setText(item.getUserSpeciality());

                item.setUsername(feedObj.getString("username"));
                item.setPassword(feedObj.getString("password"));
                item.setImgSmall(feedObj.getString("img_small"));
                item.setImgLarge(feedObj.getString("img"));
                item.setUserConnection(feedObj.getString("connection"));

1 个答案:

答案 0 :(得分:0)

我认为您应该考虑使用像Gson这样的库。如果使用Gson,您可以创建一个与Json具有相同命名的模型。

以下是参考:How can I convert JSON to a HashMap using Gson?