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

流过昼夜 提交于 2019-12-10 23:32:14

问题


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

回答1:


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, e.g. /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.




回答2:


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.



来源:https://stackoverflow.com/questions/8884658/stuck-on-parsing-a-json-response-to-a-java-object-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!