Android- App with WordPress API e GSON

i’m trying to make an android native app for my WordPress blog. I heard that WordPress has JSON-API to handle the information of a blog but I don’t know how I could use it. Please, could someone give me a tiny introduction of its use and if I could use the GSON library to manage the information within my Android app?
Thanks!!

Related posts

Leave a Reply

2 comments

  1. Simple example to parse/deserialize your JSON string

    String jsonStr = "{"name": "A", "address":"XYZ"}";
    
    // You can convert your Json string to JsonObject as below
    
    Gson gson = new Gson();
    JsonElement element = gson.fromJson (jsonStr, JsonElement.class);
    JsonObject jsonObj = element.getAsJsonObject();
    
    
    // If you can define a POJO class for your Json, you can convert json to java object
    
    public class MyData {
        private String name;
        private String address;
        //Setters and Getters   
    }
    
    Gson gson = new Gson();
    MyData data = gson.fromGson(jsonStr, MyData.class);
    

    Refer Gson API documentation for more details. Hope this will be helpful.

  2. To use GSON you will have to create a class with the exact same fields as the JSON you will want to parse. GSON till then take in a JSON string and instantiate an object with the data. For example if you have some JSON with som information like:

    {"name":"Test testsson", "id":19}
    

    and you would like to parse that. You have to create a class like:

    Person{
        public String name;
        public int id;  
    }
    

    And then use GSON:

    Gson gson = new Gson();
    Person p = gson.fromGson(jsonString, Person.class);
    

    and the p object will have the data from your JSON string.