简体   繁体   中英

Parsing in Android of WordPress JSON API

I have no error message but the parsing simply doesn't seem to work. Here's the compact JSON response :

json 响应

My "WPPost" has a problem :

    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
    }

}

The WPPostAttachement getter works well but its structure is different to the custom field structure.

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

}

How should I change the WPPost class to make the parsing work ?

You can use this online tool to convert your json object to POJO(Plain Old Java Object) type. Copy paste your json and set Source type: JSON and Annotation style: GSON . Change the package and class name the way you like. Hope this helps. If you are still unable to get the proper POJO, you can give me your json object in text format and i'll convert it to POJO. Thanks.

public JsonObject getCustomFields() {
    return custom_fields;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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