0

我有以下 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 行的正确方法。

4

0 回答 0