-2

我正在尝试从我的 JSON 文件中获取thumbPathsand 。captions

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 文件中获取正确的信息?

4

2 回答 2

1

要访问 JSON value,需要使用相应的key.

在这种情况下,结构是一个 JSON 对象,它混合了数组和键。所以根据第一个给出的结构keypictures.

这是array第一个元素包含两个键(titlegallery)的类型。我们对gallery. gallery再次是一个数组的实例(它是一个对象数组)。所以要访问我们[0]用于画廊的第一个元素来获取第一个对象。

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

例如

访问第一个实例thumbPathscaptions

Object["pictures"][0]["gallery"][0]["thumbPath"] // 将给出:""
Object["pictures"][0]["gallery"][0]["caption"] // 将给出:""

等等 ..

希望有帮助:)

于 2013-08-15T01:04:57.917 回答
1

您应该在此处查看json 包的文档。

更换类似的东西

gallery = new JSONArray(c.getString(TAG_GALLERY));

gallery = c. getJSONArray(TAG_GALLERY);

此外,如前所述,使用equals()而不是==字符串比较。

于 2013-08-15T01:10:49.503 回答