如何解析此JSON响应

时间:2013-01-02 06:21:05

标签: android json

我正在使用JSon响应来解析标题,日期内容和缩略图,并将其放在listview中。我解析了在listview中获取的标题,日期,内容和名称但是当我包含缩略图时,json解析循环以在listview中显示没有任何东西在输出中显示。任何人都可以帮我解析json响应。 JSON响应在这里

//json response to parse  
{
 "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",

                    }
                }
            },...............

以下代码用于将图像和数据添加到listview

public class CustomizedListView extends Activity {
JSONArray posts = null;

// All static variables
static final String URL = "website/ads/?json=get_recent_posts";



  static final String KEY_POSTS = "posts";
  static final String KEY_ID = "id";
  static final String KEY_TITLE = "title";
  static final String KEY_DATE = "date";
  static final String KEY_CONTENT = "content";
  static final String KEY_AUTHOR = "author";
  static final String KEY_NAME = "name";
  static final String KEY_ATTACHMENTS = "attachments";
  static final String KEY_SLUG = "slug";
  static final String KEY_THUMB_URL = "thumbnail";
  static final String KEY_IMAGES = "images";
  static final String KEY_URL = "url";

 ListView list;
    LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    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 {
        JSONArray posts = json.getJSONArray(KEY_POSTS);
        JSONArray attachments = json.getJSONArray(KEY_ATTACHMENTS);
    // 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);

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

                //loop                  
                for(int j = 0; j < attachments.length(); j++){
                    JSONObject d = attachments.getJSONObject(j);
                    String slug = c.getString(KEY_SLUG);

                    JSONObject images = d.getJSONObject(KEY_IMAGES);

                    JSONObject thumbnail = images.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);
        map.put(KEY_SLUG, slug);
        map.put(KEY_URL, url);


        // adding HashList to ArrayList
        songsList.add(map);
            }   }
            } catch (JSONException e) {
                e.printStackTrace();

                }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing json data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


        }
    });     
}   
     }

1 个答案:

答案 0 :(得分:0)

您的附件位于 json 中的帖子数组内,但在代码中,您将其保留在外部以便发布帖子。请将其保留在每个帖子中以获取每个附件。

尝试按照以下图片申请并交叉验证实施方案。

enter image description here

希望这会对你有所帮助。

相关问题