POJO类在该API中的外观必须如何: [https://dog.ceo/api/breeds/list/all][1]
我已经创建了下两个类,但是某些方法不能正确运行。
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class DogsKindsData {
@SerializedName("message")
@Expose
private List<DogKind> dogsKinds;
public List<DogKind> getDogsKinds() {
return dogsKinds;
}
}
和模型
import android.os.Parcel;
import android.os.Parcelable;
public class DogKind implements Parcelable {
private String kind;
private String imageString;
}
这是我的实现,但是不起作用。
我创建了与其他POJO类完美配合的方法
public <T> T parseInputStream(InputStream inputStream, Class<T> classType) throws IOException {
byte[] buffer = new byte[1024];
inputStream.read(buffer);
inputStream.close();
return gson.fromJson(new String(buffer, "UTF-8"), classType);
}
我以另一种方式使用该方法:
DogsKindsData dogsKindsData = jsonParser.parseInputStream(inputStream, DogsKindsData.class);
但是它说:
Process: com.andersen.dogsapp, PID: 6115
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 32 path $.message
答案 0 :(得分:0)
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class DogModel {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private Message message;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
*****************************************************************
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Message {
@SerializedName("affenpinscher")
@Expose
private List<Object> affenpinscher = null;
@SerializedName("african")
@Expose
private List<Object> african = null;
@SerializedName("airedale")
@Expose
private List<Object> airedale = null;
@SerializedName("akita")
@Expose
private List<Object> akita = null;
@SerializedName("appenzeller")
@Expose
private List<Object> appenzeller = null;
@SerializedName("basenji")
@Expose
private List<Object> basenji = null;
@SerializedName("beagle")
@Expose
private List<Object> beagle = null;
@SerializedName("bluetick")
@Expose
private List<Object> bluetick = null;
@SerializedName("borzoi")
@Expose
private List<Object> borzoi = null;
@SerializedName("bouvier")
@Expose
private List<Object> bouvier = null;
@SerializedName("boxer")
@Expose
private List<Object> boxer = null;
@SerializedName("brabancon")
@Expose
private List<Object> brabancon = null;
@SerializedName("briard")
@Expose
private List<Object> briard = null;
@SerializedName("bulldog")
@Expose
private List<String> bulldog = null;
@SerializedName("bullterrier")
@Expose
private List<String> bullterrier = null;
@SerializedName("cairn")
@Expose
private List<Object> cairn = null;
@SerializedName("cattledog")
@Expose
private List<String> cattledog = null;
@SerializedName("chihuahua")
@Expose
private List<Object> chihuahua = null;
@SerializedName("chow")
@Expose
private List<Object> chow = null;
@SerializedName("clumber")
@Expose
private List<Object> clumber = null;
@SerializedName("collie")
@Expose
private List<String> collie = null;
@SerializedName("coonhound")
@Expose
private List<Object> coonhound = null;
@SerializedName("corgi")
@Expose
private List<String> corgi = null;
@SerializedName("cotondetulear")
@Expose
private List<Object> cotondetulear = null;
@SerializedName("dachshund")
@Expose
private List<Object> dachshund = null;
@SerializedName("dalmatian")
@Expose
private List<Object> dalmatian = null;
@SerializedName("dane")
@Expose
private List<String> dane = null;
@SerializedName("deerhound")
@Expose
private List<String> deerhound = null;
@SerializedName("dhole")
@Expose
private List<Object> dhole = null;
@SerializedName("dingo")
@Expose
private List<Object> dingo = null;
@SerializedName("doberman")
@Expose
private List<Object> doberman = null;
@SerializedName("elkhound")
@Expose
private List<String> elkhound = null;
@SerializedName("entlebucher")
@Expose
private List<Object> entlebucher = null;
@SerializedName("eskimo")
@Expose
private List<Object> eskimo = null;
@SerializedName("germanshepherd")
@Expose
private List<Object> germanshepherd = null;
@SerializedName("greyhound")
@Expose
private List<String> greyhound = null;
@SerializedName("groenendael")
@Expose
private List<Object> groenendael = null;
@SerializedName("hound")
@Expose
private List<String> hound = null;
@SerializedName("husky")
@Expose
private List<Object> husky = null;
@SerializedName("keeshond")
@Expose
private List<Object> keeshond = null;
@SerializedName("kelpie")
@Expose
private List<Object> kelpie = null;
@SerializedName("komondor")
@Expose
private List<Object> komondor = null;
@SerializedName("kuvasz")
@Expose
private List<Object> kuvasz = null;
@SerializedName("labrador")
@Expose
private List<Object> labrador = null;
@SerializedName("leonberg")
@Expose
private List<Object> leonberg = null;
@SerializedName("lhasa")
@Expose
private List<Object> lhasa = null;
@SerializedName("malamute")
@Expose
private List<Object> malamute = null;
@SerializedName("malinois")
@Expose
private List<Object> malinois = null;
@SerializedName("maltese")
@Expose
private List<Object> maltese = null;
@SerializedName("mastiff")
@Expose
private List<String> mastiff = null;
@SerializedName("mexicanhairless")
@Expose
private List<Object> mexicanhairless = null;
@SerializedName("mix")
@Expose
private List<Object> mix = null;
@SerializedName("mountain")
@Expose
private List<String> mountain = null;
@SerializedName("newfoundland")
@Expose
private List<Object> newfoundland = null;
@SerializedName("otterhound")
@Expose
private List<Object> otterhound = null;
@SerializedName("papillon")
@Expose
private List<Object> papillon = null;
@SerializedName("pekinese")
@Expose
private List<Object> pekinese = null;
@SerializedName("pembroke")
@Expose
private List<Object> pembroke = null;
@SerializedName("pinscher")
@Expose
private List<String> pinscher = null;
@SerializedName("pointer")
@Expose
private List<String> pointer = null;
@SerializedName("pomeranian")
@Expose
private List<Object> pomeranian = null;
@SerializedName("poodle")
@Expose
private List<String> poodle = null;
@SerializedName("pug")
@Expose
private List<Object> pug = null;
@SerializedName("puggle")
@Expose
private List<Object> puggle = null;
@SerializedName("pyrenees")
@Expose
private List<Object> pyrenees = null;
@SerializedName("redbone")
@Expose
private List<Object> redbone = null;
@SerializedName("retriever")
@Expose
private List<String> retriever = null;
@SerializedName("ridgeback")
@Expose
private List<String> ridgeback = null;
@SerializedName("rottweiler")
@Expose
private List<Object> rottweiler = null;
@SerializedName("saluki")
@Expose
private List<Object> saluki = null;
@SerializedName("samoyed")
@Expose
private List<Object> samoyed = null;
@SerializedName("schipperke")
@Expose
private List<Object> schipperke = null;
@SerializedName("schnauzer")
@Expose
private List<String> schnauzer = null;
@SerializedName("setter")
@Expose
private List<String> setter = null;
@SerializedName("sheepdog")
@Expose
private List<String> sheepdog = null;
@SerializedName("shiba")
@Expose
private List<Object> shiba = null;
@SerializedName("shihtzu")
@Expose
private List<Object> shihtzu = null;
@SerializedName("spaniel")
@Expose
private List<String> spaniel = null;
@SerializedName("springer")
@Expose
private List<String> springer = null;
@SerializedName("stbernard")
@Expose
private List<Object> stbernard = null;
@SerializedName("terrier")
@Expose
private List<String> terrier = null;
@SerializedName("vizsla")
@Expose
private List<Object> vizsla = null;
@SerializedName("weimaraner")
@Expose
private List<Object> weimaraner = null;
@SerializedName("whippet")
@Expose
private List<Object> whippet = null;
@SerializedName("wolfhound")
@Expose
private List<String> wolfhound = null;
public List<Object> getAffenpinscher() {
return affenpinscher;
}
public void setAffenpinscher(List<Object> affenpinscher) {
this.affenpinscher = affenpinscher;
}
public List<Object> getAfrican() {
return african;
}
public void setAfrican(List<Object> african) {
this.african = african;
}
public List<Object> getAiredale() {
return airedale;
}
public void setAiredale(List<Object> airedale) {
this.airedale = airedale;
}
public List<Object> getAkita() {
return akita;
}
public void setAkita(List<Object> akita) {
this.akita = akita;
}
public List<Object> getAppenzeller() {
return appenzeller;
}
public void setAppenzeller(List<Object> appenzeller) {
this.appenzeller = appenzeller;
}
public List<Object> getBasenji() {
return basenji;
}
public void setBasenji(List<Object> basenji) {
this.basenji = basenji;
}
public List<Object> getBeagle() {
return beagle;
}
public void setBeagle(List<Object> beagle) {
this.beagle = beagle;
}
public List<Object> getBluetick() {
return bluetick;
}
public void setBluetick(List<Object> bluetick) {
this.bluetick = bluetick;
}
public List<Object> getBorzoi() {
return borzoi;
}
public void setBorzoi(List<Object> borzoi) {
this.borzoi = borzoi;
}
public List<Object> getBouvier() {
return bouvier;
}
public void setBouvier(List<Object> bouvier) {
this.bouvier = bouvier;
}
public List<Object> getBoxer() {
return boxer;
}
public void setBoxer(List<Object> boxer) {
this.boxer = boxer;
}
public List<Object> getBrabancon() {
return brabancon;
}
public void setBrabancon(List<Object> brabancon) {
this.brabancon = brabancon;
}
public List<Object> getBriard() {
return briard;
}
public void setBriard(List<Object> briard) {
this.briard = briard;
}
public List<String> getBulldog() {
return bulldog;
}
public void setBulldog(List<String> bulldog) {
this.bulldog = bulldog;
}
public List<String> getBullterrier() {
return bullterrier;
}
public void setBullterrier(List<String> bullterrier) {
this.bullterrier = bullterrier;
}
public List<Object> getCairn() {
return cairn;
}
public void setCairn(List<Object> cairn) {
this.cairn = cairn;
}
public List<String> getCattledog() {
return cattledog;
}
public void setCattledog(List<String> cattledog) {
this.cattledog = cattledog;
}
public List<Object> getChihuahua() {
return chihuahua;
}
public void setChihuahua(List<Object> chihuahua) {
this.chihuahua = chihuahua;
}
public List<Object> getChow() {
return chow;
}
public void setChow(List<Object> chow) {
this.chow = chow;
}
public List<Object> getClumber() {
return clumber;
}
public void setClumber(List<Object> clumber) {
this.clumber = clumber;
}
public List<String> getCollie() {
return collie;
}
public void setCollie(List<String> collie) {
this.collie = collie;
}
public List<Object> getCoonhound() {
return coonhound;
}
public void setCoonhound(List<Object> coonhound) {
this.coonhound = coonhound;
}
public List<String> getCorgi() {
return corgi;
}
public void setCorgi(List<String> corgi) {
this.corgi = corgi;
}
public List<Object> getCotondetulear() {
return cotondetulear;
}
public void setCotondetulear(List<Object> cotondetulear) {
this.cotondetulear = cotondetulear;
}
public List<Object> getDachshund() {
return dachshund;
}
public void setDachshund(List<Object> dachshund) {
this.dachshund = dachshund;
}
public List<Object> getDalmatian() {
return dalmatian;
}
public void setDalmatian(List<Object> dalmatian) {
this.dalmatian = dalmatian;
}
public List<String> getDane() {
return dane;
}
public void setDane(List<String> dane) {
this.dane = dane;
}
public List<String> getDeerhound() {
return deerhound;
}
public void setDeerhound(List<String> deerhound) {
this.deerhound = deerhound;
}
public List<Object> getDhole() {
return dhole;
}
public void setDhole(List<Object> dhole) {
this.dhole = dhole;
}
public List<Object> getDingo() {
return dingo;
}
public void setDingo(List<Object> dingo) {
this.dingo = dingo;
}
public List<Object> getDoberman() {
return doberman;
}
public void setDoberman(List<Object> doberman) {
this.doberman = doberman;
}
public List<String> getElkhound() {
return elkhound;
}
public void setElkhound(List<String> elkhound) {
this.elkhound = elkhound;
}
public List<Object> getEntlebucher() {
return entlebucher;
}
public void setEntlebucher(List<Object> entlebucher) {
this.entlebucher = entlebucher;
}
public List<Object> getEskimo() {
return eskimo;
}
public void setEskimo(List<Object> eskimo) {
this.eskimo = eskimo;
}
public List<Object> getGermanshepherd() {
return germanshepherd;
}
public void setGermanshepherd(List<Object> germanshepherd) {
this.germanshepherd = germanshepherd;
}
public List<String> getGreyhound() {
return greyhound;
}
public void setGreyhound(List<String> greyhound) {
this.greyhound = greyhound;
}
public List<Object> getGroenendael() {
return groenendael;
}
public void setGroenendael(List<Object> groenendael) {
this.groenendael = groenendael;
}
public List<String> getHound() {
return hound;
}
public void setHound(List<String> hound) {
this.hound = hound;
}
public List<Object> getHusky() {
return husky;
}
public void setHusky(List<Object> husky) {
this.husky = husky;
}
public List<Object> getKeeshond() {
return keeshond;
}
public void setKeeshond(List<Object> keeshond) {
this.keeshond = keeshond;
}
public List<Object> getKelpie() {
return kelpie;
}
public void setKelpie(List<Object> kelpie) {
this.kelpie = kelpie;
}
public List<Object> getKomondor() {
return komondor;
}
public void setKomondor(List<Object> komondor) {
this.komondor = komondor;
}
public List<Object> getKuvasz() {
return kuvasz;
}
public void setKuvasz(List<Object> kuvasz) {
this.kuvasz = kuvasz;
}
public List<Object> getLabrador() {
return labrador;
}
public void setLabrador(List<Object> labrador) {
this.labrador = labrador;
}
public List<Object> getLeonberg() {
return leonberg;
}
public void setLeonberg(List<Object> leonberg) {
this.leonberg = leonberg;
}
public List<Object> getLhasa() {
return lhasa;
}
public void setLhasa(List<Object> lhasa) {
this.lhasa = lhasa;
}
public List<Object> getMalamute() {
return malamute;
}
public void setMalamute(List<Object> malamute) {
this.malamute = malamute;
}
public List<Object> getMalinois() {
return malinois;
}
public void setMalinois(List<Object> malinois) {
this.malinois = malinois;
}
public List<Object> getMaltese() {
return maltese;
}
public void setMaltese(List<Object> maltese) {
this.maltese = maltese;
}
public List<String> getMastiff() {
return mastiff;
}
public void setMastiff(List<String> mastiff) {
this.mastiff = mastiff;
}
public List<Object> getMexicanhairless() {
return mexicanhairless;
}
public void setMexicanhairless(List<Object> mexicanhairless) {
this.mexicanhairless = mexicanhairless;
}
public List<Object> getMix() {
return mix;
}
public void setMix(List<Object> mix) {
this.mix = mix;
}
public List<String> getMountain() {
return mountain;
}
public void setMountain(List<String> mountain) {
this.mountain = mountain;
}
public List<Object> getNewfoundland() {
return newfoundland;
}
public void setNewfoundland(List<Object> newfoundland) {
this.newfoundland = newfoundland;
}
public List<Object> getOtterhound() {
return otterhound;
}
public void setOtterhound(List<Object> otterhound) {
this.otterhound = otterhound;
}
public List<Object> getPapillon() {
return papillon;
}
public void setPapillon(List<Object> papillon) {
this.papillon = papillon;
}
public List<Object> getPekinese() {
return pekinese;
}
public void setPekinese(List<Object> pekinese) {
this.pekinese = pekinese;
}
public List<Object> getPembroke() {
return pembroke;
}
public void setPembroke(List<Object> pembroke) {
this.pembroke = pembroke;
}
public List<String> getPinscher() {
return pinscher;
}
public void setPinscher(List<String> pinscher) {
this.pinscher = pinscher;
}
public List<String> getPointer() {
return pointer;
}
public void setPointer(List<String> pointer) {
this.pointer = pointer;
}
public List<Object> getPomeranian() {
return pomeranian;
}
public void setPomeranian(List<Object> pomeranian) {
this.pomeranian = pomeranian;
}
public List<String> getPoodle() {
return poodle;
}
public void setPoodle(List<String> poodle) {
this.poodle = poodle;
}
public List<Object> getPug() {
return pug;
}
public void setPug(List<Object> pug) {
this.pug = pug;
}
public List<Object> getPuggle() {
return puggle;
}
public void setPuggle(List<Object> puggle) {
this.puggle = puggle;
}
public List<Object> getPyrenees() {
return pyrenees;
}
public void setPyrenees(List<Object> pyrenees) {
this.pyrenees = pyrenees;
}
public List<Object> getRedbone() {
return redbone;
}
public void setRedbone(List<Object> redbone) {
this.redbone = redbone;
}
public List<String> getRetriever() {
return retriever;
}
public void setRetriever(List<String> retriever) {
this.retriever = retriever;
}
public List<String> getRidgeback() {
return ridgeback;
}
public void setRidgeback(List<String> ridgeback) {
this.ridgeback = ridgeback;
}
public List<Object> getRottweiler() {
return rottweiler;
}
public void setRottweiler(List<Object> rottweiler) {
this.rottweiler = rottweiler;
}
public List<Object> getSaluki() {
return saluki;
}
public void setSaluki(List<Object> saluki) {
this.saluki = saluki;
}
public List<Object> getSamoyed() {
return samoyed;
}
public void setSamoyed(List<Object> samoyed) {
this.samoyed = samoyed;
}
public List<Object> getSchipperke() {
return schipperke;
}
public void setSchipperke(List<Object> schipperke) {
this.schipperke = schipperke;
}
public List<String> getSchnauzer() {
return schnauzer;
}
public void setSchnauzer(List<String> schnauzer) {
this.schnauzer = schnauzer;
}
public List<String> getSetter() {
return setter;
}
public void setSetter(List<String> setter) {
this.setter = setter;
}
public List<String> getSheepdog() {
return sheepdog;
}
public void setSheepdog(List<String> sheepdog) {
this.sheepdog = sheepdog;
}
public List<Object> getShiba() {
return shiba;
}
public void setShiba(List<Object> shiba) {
this.shiba = shiba;
}
public List<Object> getShihtzu() {
return shihtzu;
}
public void setShihtzu(List<Object> shihtzu) {
this.shihtzu = shihtzu;
}
public List<String> getSpaniel() {
return spaniel;
}
public void setSpaniel(List<String> spaniel) {
this.spaniel = spaniel;
}
public List<String> getSpringer() {
return springer;
}
public void setSpringer(List<String> springer) {
this.springer = springer;
}
public List<Object> getStbernard() {
return stbernard;
}
public void setStbernard(List<Object> stbernard) {
this.stbernard = stbernard;
}
public List<String> getTerrier() {
return terrier;
}
public void setTerrier(List<String> terrier) {
this.terrier = terrier;
}
public List<Object> getVizsla() {
return vizsla;
}
public void setVizsla(List<Object> vizsla) {
this.vizsla = vizsla;
}
public List<Object> getWeimaraner() {
return weimaraner;
}
public void setWeimaraner(List<Object> weimaraner) {
this.weimaraner = weimaraner;
}
public List<Object> getWhippet() {
return whippet;
}
public void setWhippet(List<Object> whippet) {
this.whippet = whippet;
}
public List<String> getWolfhound() {
return wolfhound;
}
public void setWolfhound(List<String> wolfhound) {
this.wolfhound = wolfhound;
}
}
*******************************************************************
Parse this using GSON library
Gson gson = new Gson();
DogModel dogModel = gson.fromJson(responseString, DogModel.class);//responseString is the response received from server
/ ** * *如果您以更好的方式构造JSON会更好 * * * /