我正在尝试在解析JSON的Android应用程序中显示结果但是当我尝试使用Retrofit解析JSON时,除了字段概念之外,我最终得到了空字段。我的代码在下面。
观察类
public class Observation extends Resource implements Serializable {
@SerializedName("concept")
@Expose
private Concept concept;
@SerializedName("person")
@Expose
private Person person;
@Expose
@SerializedName("value")
private List<Object> value;
public Concept getConcept() {
return concept;
}
public void setConcept(Concept concept) {
this.concept = concept;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public List<Object> getValue() {
return value;
}
public void setValue(List<Object> value) {
this.value = value;
}
}
My Retrofit界面
public interface MyApiEndPointsInterface {
@GET("/obs/{id}")
Call<Observation> getObservation(@Path("id") String id);
}
活动中的代码
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
MyApiEndPointsInterface apiService =
retrofit.create(MyApiEndPointsInterface.class);
Call<Observation> call = apiService.getObservation(76341);
call.enqueue(new Callback<Observation>() {
@Override
public void onResponse(Response<Observation> response, Retrofit retrofit) {
Log.d("MainActivity", response.body().value.size());
}
@Override
public void onFailure(Throwable t) {
t.printStacktrace();
}
});
服务器返回的JSON对象是
{
"concept": {
"uuid": "46d4283e-3275-4c6e-9d52-cfd858889f4b",
"display": "HDU stay during admission",
"name": {
"display": "HDU stay during admission",
"uuid": "1be0302e-72fe-444c-bf0c-b92aed0c3f85",
"name": "HDU stay during admission",
"locale": "en",
"localePreferred": true,
"conceptNameType": "FULLY_SPECIFIED",
"links": [
{
"rel": "self",
"uri": "http://10.10.0.2/ws/rest/v1/concept/46d4283e-3275-4c6e-9d52-cfd858889f4b/name/1be0302e-72fe-444c-bf0c-b92aed0c3f85"
},
{
"rel": "full",
"uri": "http://10.10.0.2/ws/rest/v1/concept/46d4283e-3275-4c6e-9d52-cfd858889f4b/name/1be0302e-72fe-444c-bf0c-b92aed0c3f85?v=full"
}
],
"resourceVersion": "1.9"
}
},
"person": {
"uuid": "24d85b48-ef69-4de0-91a4-e751bda85332",
"display": "103 - Gavana Test Nkaiseri",
"links": [
{
"rel": "self",
"uri": "http://10.10.0.2/ws/rest/v1/patient/24d85b48-ef69-4de0-91a4-e751bda85332"
}
]
},
"value": {
"uuid": "1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"display": "No",
"name": {
"display": "No",
"uuid": "1138BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
"name": "No",
"locale": "en",
"localePreferred": true,
"conceptNameType": "FULLY_SPECIFIED",
"links": [
{
"rel": "self",
"uri": "http://10.10.0.2/ws/rest/v1/concept/1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/name/1138BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
},
{
"rel": "full",
"uri": "http://10.10.0.2/ws/rest/v1/concept/1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/name/1138BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB?v=full"
}
],
"resourceVersion": "1.9"
},
"datatype": {
"uuid": "8d4a4c94-c2cc-11de-8d13-0010c6dffd0f",
"display": "N/A",
"links": [
{
"rel": "self",
"uri": "http://10.10.0.2/ws/rest/v1/conceptdatatype/8d4a4c94-c2cc-11de-8d13-0010c6dffd0f"
}
]
},
"conceptClass": {
"uuid": "8d492774-c2cc-11de-8d13-0010c6dffd0f",
"display": "Misc",
"links": [
{
"rel": "self",
"uri": "http://10.10.0.2/ws/rest/v1/conceptclass/8d492774-c2cc-11de-8d13-0010c6dffd0f"
}
]
}
}
}
答案 0 :(得分:2)
对于更复杂的json结构,不要依赖于json到pojo生成器。手工做。您的POJO类有一些错误。此外,当您使用GsonConverterFactory
时,您不需要使用getter和setter,只需将字段设为公开即可。这些模型将正确反序列化(我将根类JsonObj
命名为因为我不知道我们正在处理什么类型的数据):
public class Observation {
public Concept concept;
public DataDescription person;
public Value value;
}
public class Concept {
public String uuid;
public String display;
public Name name;
}
public class Name {
public String uuid;
public String display;
public String name;
public String locale;
public String localePreferred;
public String conceptNameType;
public Link[] links;
public String resourceVersion;
}
public class Link {
public String rel;
public String uri;
}
public class DataDescription {
public String uuid;
public String display;
public Link[] links;
}
public class Value {
public String uuid;
public String display;
public Name name;
public DataDescription datatype;
public DataDescription conceptClass;
}
然后,您可以在Retrofit界面中反序列化此项:
Call<Observation> getObservation(@Path("id") String id);