无法显示我在Android中从JSON解析的数据

时间:2012-11-21 09:57:35

标签: android json

大家好我正在尝试显示我从JSON字符串中获取的数据,其中包含名称,图像和内容(textview,imageview和textview)。我能够从JSON获取数据,但我不是能够显示它,甚至我没有在我的Logcat中收到任何错误。

我正在尝试的代码是

try {
     bookDetails = json.getJSONArray(TAG_DETAILS);
     for (int i = 0; i < bookDetails.length(); i++) 
     {
     JSONObject c = bookDetails.getJSONObject(i);
     String image1 = c.getString(TAG_IMAGE1);
     String image2=c.getString(TAG_IMAGE2);
     String Bid = c.getString(TAG_ID);
     String Bname = c.getString(TAG_NAME);
     String Bdesc= c.getString(TAG_DESCRIPTION);
     String BbookId=c.getString(TAG_BOOKID);
     HashMap<String, String> map = new HashMap<String, String>();
     map.put(TAG_IMAGE1, image1);
     map.put(TAG_IMAGE2, image2);
     map.put(TAG_ID,Bid);
     map.put(TAG_NAME,Bname);
     map.put(TAG_DESCRIPTION,Bdesc);
     map.put(TAG_BOOKID,BbookId);
     booksList.add(map);
     String[] captionArray = (String[]) ((List<NameValuePair>) map).toArray(new String[map.size()]);
     ItemsAdapter itemsAdapter=new ItemsAdapter(DetailsActivity.this, R.layout.details,booksList);
     setListAdapter(itemsAdapter);
     }
       } catch (JSONException e)
         {
             e.printStackTrace();
          }
            return null;
   }

和我的适配器是

 private class ItemsAdapter extends BaseAdapter implements ListAdapter
        {
            String[] items;


            public ItemsAdapter(DetailsActivity detailsActivity,
                    int details, ArrayList<HashMap<String, String>> booksList) {
                // TODO Auto-generated constructor stub
                this.items = items;
            }
            public View getView( int POSITION, View convertView,ViewGroup parent)
            {
                TextView cap = null ;
                View view = convertView;
                ImageView img = null;
                if (view == null) 
                {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.details, null);
                }
                img = (ImageView) view.findViewById(R.id.smallImage);
                Log.i("sss","sss");
                cap = (TextView) view.findViewById(R.id.caption);
                cap.setText(mapName.get(POSITION));
                img.setImageBitmap(BitmapFactory.decodeByteArray(map.get(POSITION), 0, map.get(POSITION).length));
                return view;
            }
            public int getCount() 
            {
                return items.length;
            }
            public Object getItem(int position) 
            {
                return position;
            }
            public long getItemId(int position) 
            {
                return position;
            }
        }

我的JSON字符串是

{
    "bookdetails": [
        {
            "bid": "2",
            "name": "Android 2 Application Dev",
            "bookid": "1",
            "description": "Providing in-depth coverage of how to build mobile applications using the next major release of the Android SDK, this invaluable resource takes a hands-on approach to discussing Android with a series of projects, each of which introduces a new feature and highlights techniques and best practices to get the most out of Android.",
            "image1": "http://media.shelf-awareness.com/shelfcontest.png",
            "image2": "http://cache0.bdcdn.net/assets/images/book/medium/9780/4704/9780470452622.jpg"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

你错过了JsonObject和JsonArray,只需使用这个

JSONObject mJsonObject=null;
JSONArray bookDetails = null;
try {
    mJsonObject= new JSONObject("YourJson");
    bookDetails = mJsonObject.getJSONArray("bookdetails");
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

try {
    System.out.println("bookDetails.length() : "+bookDetails.length());
    for (int i = 0; i < bookDetails.length(); i++) 
    {
        JSONObject c = bookDetails.getJSONObject(i);

        String image1 = c.getString("image1");
        String image2=c.getString("image2");
        String Bid = c.getString("bid");
        String Bname = c.getString("name");
        String Bdesc= c.getString("description");
        String BbookId=c.getString("bookid");
        }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}