이것은 일련의 역 직렬화 질문 중 하나이지만 모두 읽었으며 내 문제에 대한 해결책을 찾을 수 없습니다.
모든 "entery"-> "content"-> $ t 및 "entery"-> "title"-> "$ t"를 가져와야하지만 CategoryDeserializer ()에서는 NULL 인 JsonArray를 얻습니다. "<===="로 코드의 해당 부분을 가리 켰습니다.
에러 메시지:
널 오브젝트 참조에서 인터페이스 메소드 'java.util.Iterator java.util.List.iterator ()'호출 시도
다음과 같은 JSON이 있습니다.
{ "feed":{
"id":{ ... },
"author":[ ... ],
"entry":[
{
"id":{ },
"updated":{ },
"category":[ ],
"title":{
"type":"text",
"$t":"A1 },
"content":{
"type":"text",
"$t":"test"
},
"link":[ ]
},
{ ... },
{ ... },
{ ...},
]
}
}
이것은 "entery"를 역 직렬화하려는 내 코드의 일부입니다.
String json = response.body().toString();
Type listType = new TypeToken<List<Entry>>() {
}.getType();
Gson gson = new GsonBuilder().registerTypeAdapter(listType, new CategoryDeserializer()).create();
List<Entry> list = gson.fromJson(json, listType);
for (Entry entry : list) {
Log.i("MainActivity", "Content: " + entry.getContent());}
CategoryDeserializer ()는 다음과 같습니다.
public class CategoryDeserializer implements JsonDeserializer<List<Entry>> {
public List<Entry> deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
JsonArray entry = je.getAsJsonObject().getAsJsonArray("entry"); //<==== here I get that entry is null but je has a value
ArrayList<Entry> myList = new ArrayList<Entry>();
for (JsonElement e : entry) {
myList.add((Entry) jdc.deserialize(e, Entry.class));
}
return myList;}
그리고 내 엔트리 클래스 :
public class Entry {
private Id_ id;
private Updated_ updated;
private List<Category_> category = null;
private Title_ title;
private Content content;
private List<Link_> link = null;
//getters and setters
public Id_ getId() {
return id;
}
public void setId(Id_ id) {
this.id = id;
}
public Updated_ getUpdated() {
return updated;
}
public void setUpdated(Updated_ updated) {
this.updated = updated;
}
public List<Category_> getCategory() {
return category;
}
public void setCategory(List<Category_> category) {
this.category = category;
}
public Title_ getTitle() {
return title;
}
public void setTitle(Title_ title) {
this.title = title;
}
public Content getContent() {
return content;
}
public void setContent(Content content) {
this.content = content;
}
public List<Link_> getLink() {
return link;
}
public void setLink(List<Link_> link) {
this.link = link;
}
}
편집 : 선언 및 인스턴스화가 있습니다.
복잡하지 않은 것들을 복잡하게 만들고있었습니다. 내가해야 할 일은 다음과 같습니다.
String json = response.body().toString();
Gson mGson = new Gson();
//where Example is the root of JSON
Example rsp = mGson.fromJson(json, Example.class);
//Entry is the list I needed to access
List <Entry> listOfEntrys= rsp.getFeed().getEntry();
//get value
Log.i("MainActivity", "listaEntrya " + listOfEntrys.get(0).getTitle().get$t());
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다