Android JSON嵌套数组

Android JSON嵌套数组,android,json,Android,Json,我试着模仿这类问题中选择的答案,但我不确定为什么我无法检索到这类问题中“附件”的URL 我所追求的是一种获取每个“帖子”,然后抓取每个“附件”图像URL并保存为字符串的方法。我试着在我的模拟器上这样做,但它只是暂停和永远运行。由于某些原因,我无法将我的真实手机也用作调试器,否则我会发布日志 有一件事我可以肯定的是,除了附件,一切都是正确的。我已经设法下载了帖子,但无法获得任何嵌套的内容。我是JSON新手,因此非常感谢您的帮助 我的异步: // you can make this class a

我试着模仿这类问题中选择的答案,但我不确定为什么我无法检索到这类问题中“附件”的URL

我所追求的是一种获取每个“帖子”,然后抓取每个“附件”图像URL并保存为字符串的方法。我试着在我的模拟器上这样做,但它只是暂停和永远运行。由于某些原因,我无法将我的真实手机也用作调试器,否则我会发布日志

有一件事我可以肯定的是,除了附件,一切都是正确的。我已经设法下载了帖子,但无法获得任何嵌套的内容。我是JSON新手,因此非常感谢您的帮助

我的异步:

 // you can make this class as another java file so it will be separated from your main activity.
        // https://www.codeofaninja.com/2013/11/android-json-parsing-tutorial.html
        public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

            private ArrayList<RssFeedItem> tempArray = new ArrayList<RssFeedItem>();
            final String TAG = "AsyncTaskParseJson";
            private ProgressDialog progress;

            // set your json string url here
            String yourJsonStringUrl = "http://www.prindlepost.org/?json=tag_slug=java";

            // contacts JSONArray
            JSONArray dataJsonArr = null;
            JSONArray imageURLArr = null;

            @Override
            protected void onPreExecute() {
                progress = new ProgressDialog(getActivity());
                progress.setTitle("Downloading Prindle's Posts");
                progress.setMessage("This should just take a moment.");
                progress.show();
            }

            @Override
            protected String doInBackground(String... arg0)
            {
                try
                {
                    // instantiate our json parser
                    JsonParser jParser = new JsonParser();

                    // get json string from url
                    JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                    // get the array of users
                    dataJsonArr = json.getJSONArray("posts");

                    // loop through all users
                    for (int i = 0; i < dataJsonArr.length(); i++)
                    {
                        JSONObject c = dataJsonArr.getJSONObject(i);
                        // Storing each json item in variable
                        String id = c.getString("id");
                        String type = c.getString("type");
                        String slug = c.getString("slug");
                        String title = c.getString("title");
                        String content = c.getString("content");
                        String author = c.getString("author");

                        //http://stackoverflow.com/questions/19748829/android-get-json-array-nested-in-array
                        JSONObject attachments = c.getJSONObject("attachments");

                        Log.d("attachment",""+attachments.getString("url"));


                        // show the values in our logcat
                        Log.e(TAG, "id: " + id
                                + ", type: " + type
                                + ", slug: " + slug
                                + ", title: " + title
                                + ", author: " + author
                                + ", content: " + content + "\n\n");

                        tempArray.add(new RssFeedItem(title, content, "", 0, new Date(), author));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

您可以使用getJSONArray()方法。

我也有同样的问题。几天后,我的大脑开始融化,我尝试使用谷歌的GSON。它为您完成所有解析和思考,并返回一个漂亮的小对象,其中包含来自JSON的所有信息

以下是项目链接:

要使用它,您必须实例化一个新的
Gson
解析器,如下所示

Gson gson = new Gson();
YourObject object = gson.fromJson(jsonString, YourObject.class);
YourObject
类应该如下所示:

public class YourObject{
    int status;
    int count;
    String count_total;
    ...
    Post[] posts;
}
现在,使用JSON中预测的字段创建一个
Post
类:

public class Post{
    int id;
    String type;
    String slug;
    ...
    Category[] categories;
}
我想你可以知道如何设置你的POJO。请记住,如果在JSON中获得一个对象数组作为基本对象,请确保在调用
gson.fromJson
时使用
YourObject[]
而不是
YourObject


请注意:如果任何Json元素都有
null
或空值,即使它们主要是
YourObject
类中的
int
,最好将它们声明为
String
以避免
java.lang.NumberFormatException

对于JSON中更复杂的结构,我绝对建议使用GSON-通用JSON“阅读器”在这种情况下很笨拙,代码很难维护哇!我要调查一下。我没有在我的任何发现中看到GSON,但这确实很有趣。如果只写对象,然后让它们将信息导入其中,那就太好了
public class Post{
    int id;
    String type;
    String slug;
    ...
    Category[] categories;
}