0

我很困惑如何在另一个数组中为 Json 响应数组使用循环。以下是我的 jsonresponse 如何循环此以获取列表视图中的数据和图像

`{
"status": "ok", 

    "posts": [
        {
            "id": 2498,
            "title": "jigsaw lamp imported from thailand",
            "content": "<p>Hi. It&#8217;s a invitation to have a look at a unique lamp shade called jigsaw lamp from thailand. Available in multi attractive colours.</p>\n",
            "date": "2012-12-26 09:48:15",
             "author": {
                "name": "Tapas123456",
                            },
                "attachments": [
                {
                    "description": "",
                    "caption": "",
                    "mime_type": "image/jpeg",
                    "images": {
                    "thumbnail": {
                    "url": "http://site/wp-content/uploads/2012/12/646675-50x47.jpg",
                        }
                    }
                  ]
                },........

以下是我用来循环的代码这是正确的做法吗?

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


    // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(URL);
            try {
            posts = json.getJSONArray(KEY_POSTS);
    // looping through all song nodes <song>
            for(int i = 0; i < posts.length(); i++){
                JSONObject c = posts.getJSONObject(i);
                // Storing each json item in variable
                String id = c.getString(KEY_ID);
                String title = c.getString(KEY_TITLE);
                String date = c.getString(KEY_DATE);
                String content = c.getString(KEY_CONTENT);

                // Phone number is agin  JSON Object
                JSONObject author = c.getJSONObject(KEY_AUTHOR);
                String name = author.getString(KEY_NAME);

                JSONArray attachments = json.getJSONArray(KEY_ATTACHMENTS);

                for(int j = 0; j < attachments.length(); j++){
                    JSONObject d = attachments.getJSONObject(j);
                    JSONObject images = d.getJSONObject(KEY_IMAGES);

                    JSONObject thumbnail = d.getJSONObject(KEY_THUMB_URL);
                    String url = thumbnail.getString(KEY_URL);
                }
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
        map.put(KEY_ID, id);
        map.put(KEY_TITLE, title);
        map.put(KEY_DATE, date);
        map.put(KEY_NAME, name);
        map.put(KEY_CONTENT, content);



        // adding HashList to ArrayList
        songsList.add(map);
4

1 回答 1

0

只需替换以下代码

for(int j = 0; j < attachments.length(); j++){
                    JSONObject d = attachments.getJSONObject(j);
                    JSONObject images = d.getJSONObject(KEY_IMAGES);

                    JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                    String url = thumbnail.getString(KEY_URL);
                }

而不是旧代码。

于 2012-12-29T12:19:37.753 回答