JSON无法访问我的对象

时间:2013-08-15 00:59:14

标签: android json

我正在尝试从我的JSON文件中获取thumbPathscaptions

JSON文件:

"pictures": [

        {

            "title": "Animals", 
            "gallery": 

            [

                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                }

            ]

        },
        {

            "title": "Auroras", 
            "gallery": 

            [

                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                }

            ]

        },
        {

            "title": "Boats", 
            "gallery": 

            [

                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                },
                {
                    "path":"",
                    "thumbPath":"",
                    "caption":""
                }

            ]

        }

我试图将它们保存到自己的数组中,以便我可以处理从thumbPaths获取图像。 我试图在AsyncTask

中实现这一点
public class getJSONObjects extends AsyncTask<String, Void, String[]>{

        @Override
        protected String[] doInBackground(String... URL) {
            // Access the JSONHandler for the URL
            Log.d(tag, "in background on getJSON Artist Gallery.");
            //initalize parser
            JSONParser jparse = new JSONParser();
            //call objects          
                JSONObject json = jparse.getJSONFromUrl(URL);
                try {
                    pictures = new JSONArray(json.getString(TAG_PICTURES));
                    Log.d(tag, "after pictures");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // looping through All Pictures
                for(int i = 0; i < pictures.length(); i++){
                    Log.d(tag, "in loop for pictures");
                    Log.d(tag, "Index:" + i);
                    JSONObject c;
                    try {
                        c = pictures.getJSONObject(i);
                        String title2 = c.getString(TAG_TITLE);
                        titles[i] = title2;
                        gallery = new JSONArray(c.getString(TAG_GALLERY));
                        Log.d(tag, "just got gallery array");
                        Log.d(tag, "before if statement");
                        Log.d(tag, "title:" + titles[i].toString());
                        if(title2 == titleTextView.getText().toString()){
                            Log.d(tag, "inside if statement");
                            for(int z = 0; z < gallery.length(); z++){
                                try {
                                    JSONObject d = gallery.getJSONObject(z);
                                    String thumbPath = d.getString(TAG_THUMBPATHS);
                                    String captions = d.getString(TAG_CAPTIONS);
                                    Log.d(tag, "thumbPath:" + thumbPath);
                                    Log.d(tag, "captions:" + captions);

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

在上面我试图说,如果标题是== JSON数组中的标题,它应该只抓取该标题下的库中的thumbPaths和标题。这样我只能抓住我需要的东西,而不是更多。

如何从JSON文件中获取正确的信息?

2 个答案:

答案 0 :(得分:1)

要访问JSON value,需要使用相应的key访问它们。

在这种情况下,结构是一个JSON对象,它包含数组和键的混合。因此,根据给定的结构,第一个keypictures

此类型为array,其第一个元素包含两个键(titlegallery)。我们对gallery感兴趣。 gallery又是一个数组的实例(它是一个对象数组)。为了访问第一个元素,我们使用[0]来获取第一个对象。

该对象由3个键值对组成。您可以使用object[key]通过相应的键访问它们以获取特定值。

例如

要访问thumbPathscaptions

的第一个实例
Object["pictures"][0]["gallery"][0]["thumbPath"]  // will give : ""
Object["pictures"][0]["gallery"][0]["caption"]    // will give : ""

依旧......

希望有所帮助:)

答案 1 :(得分:1)

您应该查看json包here的文档。

替换

之类的内容
gallery = new JSONArray(c.getString(TAG_GALLERY));

gallery = c. getJSONArray(TAG_GALLERY);

另外,如前所述,使用equals()而非==进行字符串比较。