getJSONArray语法

时间:2012-10-24 18:18:17

标签: android json

我有以下JSON

[{"name":"Games","id":1,"thumbnail":"https:\/\/lh5.ggpht.com\/_nl17ca8wUp0BbiD9J7mTBSO1o42KpdK2IolG3NjF22o1KbhIZ6ga5e_cXPp42fNUjA=w78","image_medium":"https:\/\/lh5.ggpht.com\/_nl17ca8wUp0BbiD9J7mTBSO1o42KpdK2IolG3NjF22o1KbhIZ6ga5e_cXPp42fNUjA=w78","image_large":"https:\/\/lh5.ggpht.com\/_nl17ca8wUp0BbiD9J7mTBSO1o42KpdK2IolG3NjF22o1KbhIZ6ga5e_cXPp42fNUjA=w78"},{"name":"Sports","id":2,"thumbnail":"https:\/\/lh4.ggpht.com\/yvn4iHEzWN7NmwVkw08ufwSS86mYPpK2Z8WgYkwkQqojMTPsTs28tIiz4v780KGQfrA=w78","image_medium":"https:\/\/lh4.ggpht.com\/yvn4iHEzWN7NmwVkw08ufwSS86mYPpK2Z8WgYkwkQqojMTPsTs28tIiz4v780KGQfrA=w78","image_large":"https:\/\/lh4.ggpht.com\/yvn4iHEzWN7NmwVkw08ufwSS86mYPpK2Z8WgYkwkQqojMTPsTs28tIiz4v780KGQfrA=w78"}]

以下是阅读上述json的代码片段

    private static final String TAG_CATEGORY_NAME = "name";
    private static final String TAG_CATEGORY_ID = "id";
    private static final String TAG_CATEGORY_THUMBNAIL = "thumbnail";
    private static final String TAG_CATEGORY_IMAGE_MEDIUM = "image_medium";
    private static final String TAG_CATEGORY_IMAGE_LARGE = "image_large";

try
        {
            //Getting Array of Categories
            **categories = json.getJSONArray(name);**   //Line 1

            for(int i=0; i < categories.length(); i++)
            {
                JSONObject c = categories.getJSONObject(i);

                String cname = c.getString(TAG_CATEGORY_NAME);
                String cid = c.getString(TAG_CATEGORY_ID);
                String cThumbNail = c.getString(TAG_CATEGORY_THUMBNAIL);
                String cImageMedium = c.getString(TAG_CATEGORY_IMAGE_MEDIUM);
                String cImageLarge = c.getString(TAG_CATEGORY_IMAGE_LARGE);
            }
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }

由于没有给出代表json中每个数组的名称,任何人都可以告诉我在这种情况下编写第1行的方法。

0 个答案:

没有答案