如何获取内部数组数据

时间:2014-04-03 10:52:25

标签: android listview

我正在构建一个Android应用程序并从我的远程API收集数据(使用Volley) 我需要解析响应。我知道如何获得“项目”数组,但我如何获得每个项目的标题(请注意每个项目的项目密钥)。

{
  "code": 200,
  "total": 4,
  "projects": [
    {
      "project": {
        "id": 1,
        "title": "A nice long title",
        "latitude": 56.0293783,
        "longitude": 12.7256732,
        "created_at": "2013-10-20T20:57:00+02:00",
        "created_at_human": "5 months",
        "total_tasks": 7,
        "description": "This is a description.",
        "address": "simple highway 22",
        "zipcode": "25656",
        "city": "florida"
      }
    },
    {
      "project": {
        "id": 2,
        "title": "A nice long title",
        "latitude": 56.0293783,
        "longitude": 12.7256732,
        "created_at": "2013-10-20T20:57:00+02:00",
        "created_at_human": "5 months",
        "total_tasks": 7,
        "description": "This is a description.",
        "address": "simple highway 22",
        "zipcode": "25656",
        "city": "florida"
      }
    }
  ]
}

这是我现在使用的代码,需要修改:

JSONArray jsonPosts = mData.getJSONArray("projects");

            ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();

            for (int i = 0; i < jsonPosts.length(); i++) {

                JSONObject post = jsonPosts.getJSONObject(i);

                Log.e("OUTPUT", "THE POST: " + post);

            }

输出:

E/OUTPUT﹕ THE POST: {"project":{"id":1,"title":"A nice long title","total_tasks":7,"address":"simple highway 22","description":"This is a description.","zipcode":"25656","created_at":"2013-10-20T20:57:00+02:00","longitude":12.7256732,"created_at_human":"5 MåNADER","latitude":56.0293783,"city":"florida"}}

如何访问每个标题?

2 个答案:

答案 0 :(得分:1)

这是JSONArray而不是JSONObject - 从中​​JSONObject,使用

JSONObject jsonObject = jsonArray.getJSONObject(0);

这将从此JSONArray中获取第一个JSONObject。

如果您有多个JSONObjects,请使用:

JSONObject jsonObject;
for(int n = 0; n < jsonArray.length(); n++)
{
    jsonObject = jsonArray.getJSONObject(n);
}

获取值:

jsonObject.getString("name");

答案 1 :(得分:1)

每个jSONObject项都包含一个键和jSONObject。我认为您需要从Array中的item获取jSONObject然后获取title和city。

我希望它对你有帮助........

JSONArray jsonPosts = mData.getJSONArray("projects");
                ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();

            for (int i = 0; i < jsonPosts.length(); i++) {

                JSONObject post = jsonPosts.getJSONObject(i);
                JSONObject innerjson = post.getJSONObject("project");

                String title = innerjson.getString(KEY_TITLE);
                       title = Html.fromHtml(title).toString();

                String city = innerjson.getString(KEY_CITY);
                       city = Html.fromHtml(city).toString();

                HashMap<String, String> blogPost = new HashMap<String, String>();

                blogPost.put(KEY_TITLE, title);
                blogPost.put(KEY_CITY, city);
                blogPosts.add(blogPost);

            }

            String[] keys = {KEY_TITLE, KEY_CITY};
            int[] ids = { R.id.top_label, R.id.bottom_label};

            SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, R.layout.list_item, keys, ids);
            setListAdapter(adapter);