【问题标题】:how to fetch tags from wordpress posts JSON results in android Java如何从wordpress帖子中获取标签JSON结果在android Java中
【发布时间】:2017-02-11 10:34:31
【问题描述】:

我正在尝试制作一个 android 应用程序,该应用程序可以从我的 wordpress 博客中获取帖子,并在列表中显示信息。我能够获得标题、描述等结果,但我无法从 JSON 结果中获得嵌套对象“标签”。那么,你能解释一下我如何从这个JSON Response 的 JSON 结果中获取标签名称。

我正在尝试使用以下代码:

JSONObject root = new JSONObject(postJSON);
        JSONArray postsArray = root.getJSONArray("posts");


        for (int i = 0; i < postsArray.length(); i++) {
            // Get a single post at position i within the list of earthquakes
            JSONObject currentPost = postsArray.getJSONObject(i);

            String title = currentPost.getString("title");
            Log.e(LOG_TAG, "title is " + title);

            JSONObject tags = currentPost.getJSONArray("tags").getJSONObject(0);
            String tag = tags.getString("name");
            Log.e(LOG_TAG, "tag is " + tag);

            Post post = new Post(title,"123", tag);
            posts.add(post);
        }

但是 logcat 显示该值无法转换为 JSONArray。

【问题讨论】:

    标签: java android json wordpress


    【解决方案1】:

    问题在于标签实际上是另一个 JSON 对象而不是 JSON 数组。您需要执行以下操作:

    JSONObject tags = currentPost.getJSONObject("tags").getJSONObject(0);
    String tag = tags.getString("name");
    Log.e(LOG_TAG, "tag is " + tag);
    

    请记住,JSON 数组始终由 [] 表示,对象由 {} 表示。

    希望这会有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    相关资源
    最近更新 更多