【问题标题】:Parsing in Android of WordPress JSON API在Android中解析WordPress JSON API
【发布时间】:2016-05-05 04:44:15
【问题描述】:

我没有错误消息,但解析似乎根本不起作用。 这是紧凑的 JSON 响应:

我的“WPPost”有问题:

    public class WPPost {

    int id;
    String title;
    String content;
    ArrayList<WPPostAttachment> attachments;
    WPPostCustomFields custom_fields;

    public String getContent() {
        return content;
    }

    public String getTitle() {
        return title;
    }

    public int getId() {
        return id;
    }

    public ArrayList<WPPostAttachment> getAttachments() {
        return attachments;
    }

    public WPPostCustomFields getCustomFields() {
        return custom_fields; //Problem here
    }

}

WPPostAttachement getter 运行良好,但其结构与自定义字段结构不同。

    public class WPPostAttachment {

    String id;
    String url;
    String title;

    public String getId() {
        return id;
    }

    public String getUrl() {
        return url;
    }

    public String getTitle() {
        return title;
    }

}

    public class WPPostCustomFields {

    String address;

    public String getAddress() {
        return address;
    }

}

我应该如何更改 WPPost 类以使解析工作?

【问题讨论】:

  • 能否提供上述json对象的文本而不是image?
  • 只需在此处复制并粘贴您的 json pojo.sodhanalibrary.com
  • 这是关于解析的:“custom_fields”:{“address”:[“230 Bach Dang st,Da Nang,Vietnam”],“ratings_users”:[“1”],“ratings_score”:[ "5" ], "ratings_average": [ "5" ] }

标签: android json wordpress gson android-volley


【解决方案1】:

您可以使用this在线工具将您的json对象转换为POJO(Plain Old Java Object)类型。 复制粘贴您的 json 并设置源类型:JSON 和注释样式:GSON。以您喜欢的方式更改包和类名。希望这可以帮助。如果您仍然无法获得正确的 POJO,您可以将文本格式的 json 对象提供给我,我会将其转换为 POJO。谢谢。

【讨论】:

  • 它可以在不破坏解析的情况下转换我收到的 JSON 吗?
  • 你现在不需要做任何解析。只需使用 google 自己的 volley 库并发出 GSON 请求调用,您只需在其中传递主模型类的名称即可。 Json 值将自动在模型的设置器中设置。
【解决方案2】:
public JsonObject getCustomFields() {
    return custom_fields;
}

【讨论】:

  • 请不要只是添加一些代码,还要解释你在做什么以及为什么这会对 OP 有所帮助
猜你喜欢
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
相关资源
最近更新 更多