简体   繁体   English

如何在android Java中从wordpress帖子JSON结果中获取标签

[英]how to fetch tags from wordpress posts JSON results in android Java

I am trying to make an android app that fetch posts from my wordpress blog that displays the information in a list.我正在尝试制作一个 android 应用程序,它从我的 wordpress 博客中获取帖子,并在列表中显示信息。 I am able to get the results like title, description,etc but i am not able to get nested object "tags" from the JSON result.我能够获得标题、描述等结果,但我无法从 JSON 结果中获得嵌套对象“标签”。 So, can you explain me how i can get the tag names from the JSON result from this JSON Response .所以,你能解释一下我如何从这个JSON Response的 JSON 结果中获取标签名称。

I am trying to use the following code :我正在尝试使用以下代码:

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);
        }

But the logcat is showing that the value can't be converted to JSONArray.但是 logcat 显示该值无法转换为 JSONArray。

The issue is that tags is actually another JSON object not a JSON array.问题是标签实际上是另一个 JSON 对象而不是 JSON 数组。 You need to do the following:您需要执行以下操作:

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

Just remember JSON arrays are always denoted by [] and objects by {}.请记住,JSON 数组始终用 [] 表示,对象始终用 {} 表示。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM