How to parse complex JSON file in android

后端 未结 2 679
醉酒成梦
醉酒成梦 2020-12-03 16:34

I need to parse this json string

 {
\"results\": {
    \"result\": [
        {
            \"cover\": \"http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5         


        
相关标签:
2条回答
  • 2020-12-03 17:16

    First of all please look at some tutorials like:

    http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

    http://www.vogella.com/articles/AndroidJSON/article.html

    JSONObject response = new JSONObject(respString);
    ArrayList<Book> bookCollection = new ArrayList<Book>();//
        if(response.has("results")){
            if(response.has("result")){
                JSONArray resultArray = response.getJSONArray("result");
                for(int iCount=0; iCount<resultArray.length; iCount++){
                    JSONObject item = resultArray.getJSONObject(iCount);
                    Book book = new Book();
                    if(item.has("title")){
                        book.title =item.getString("title");
                    }
                    bookCollection.add(book);
                }
            }
        }
    

    The pojo to populate

    public Book{
    
    public String cover;
    public String title;
    
    }
    
    0 讨论(0)
  • 2020-12-03 17:21

    You can try to Parse Current Json String as:

    JSONObject response = new JSONObject(respString);
    JSONObject jsonobjresults = response.getJSONObject("results");
    JSONArray resultArray = jsonobjresults.getJSONArray("result");
    Map<Integer,ArrayList<String>> 
                           mapoflisp = new HashMap<Integer,ArrayList<String>>();  
    ArrayList<String> listofitem;
    for(int i=0; i<resultArray.length; i++){
        JSONObject itemobj = resultArray.getJSONObject(i);
        listofitem=new ArrayList<String>();
        // Add items in List
        listofitem.add(item.getString("cover"));
        listofitem.add(item.getString("title"));
        listofitem.add(item.getString("year"));
        listofitem.add(item.getString("director"));
        listofitem.add(item.getString("rating"));
        listofitem.add(item.getString("details"));
    
         //add list to Map
        mapoflisp.put(i,listofitem); 
    
        // your code here...
     }  
    
    0 讨论(0)
提交回复
热议问题