简体   繁体   中英

Stuck on parsing a JSON response to a Java Object in Android

I am making a query call to freebase and I receive a JSON response. The response has the following structure:

{
  "code":          "/api/status/ok",
  "result": [
    {
      "/common/topic/image": [{
        "guid": "#9202a8c04000641f8000000004b67f6d"
      }],
      "/people/person/profession": [{
        "name": "Critic"
      }],
      "id":   "/en/michael_jackson_1942",
      "name": "Michael Jackson",
      "type": "/people/person"
    },  
    {
      "/common/topic/image": [{
        "guid": "#9202a8c04000641f800000001b90fdea"
      }],
      "/people/person/profession": [{
        "name": "Actor"
      }],
      "id":   "/en/michael_jackson_1970",
      "name": "Michael Jackson",
      "type": "/people/person"
    }
  ],
  "status":        "200 OK",
  "transaction_id": "cache;cache03.p01.sjc1:8101;2012-01-16T18:28:36Z;0055"
}

I need to parse this response in a ArrayList of java objects using GSON. To do this I need to create the class of the object with get/set and make it available to parse. Or is there another simpler way to do things ? I have used simple JSON strings by now, but in this case I can't remake the structure of the class I need. Basically in the end I need something like ArrayList<Person> where Person has all the attributes from the json string.

Any help is appreciated. Thank you.

The final solution, according with the answer below

public class FreebaseResponse {
    @SerializedName("code")
    public String code;

    @SerializedName("result")
    public ArrayList<Person> result;

    @SerializedName("status")
    public String status;

    @SerializedName("transaction_id")
    public String transaction_id;
}

public class Person {
    @SerializedName("/common/topic/image")
    public ArrayList<Person.Guid> imageGuid;

    @SerializedName("/people/person/profession")
    public  ArrayList<Person.Profession> profession;

    @SerializedName("id")
    public String id;

    @SerializedName("name")
    public String name;

    @SerializedName("type")
    public String type;

    private class Guid
    {
        @SerializedName("guid")
        public String guid;
    }

    private class Profession
    {
        @SerializedName("name")
        public String name;
    }
}

If you need all the fields, the way you mentioned seems to me like the way to go. If you only want a small part of the data, you can get it directly. In general, I think that since JSON is meant for object representation, it is better to create the appropriate class. It will ease your way in the future as well, as you will need more from this data.

I guess you can create a FreebaseResponse class that contains code , result ( ArrayList<Person> ), etc fields and use Gson to deserialize. the names that are not valid identifiers, eg /common/topic/image will be a problem. I haven't tried it myself but it seems to me that SerializedName annotation should do the trick.

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