为什么我得到一个JsonSyntaxException:预期BEGIN_OBJECT但是STRING在第1行第24列路径$ .result?

时间:2016-11-01 07:19:04

标签: string gson

1.我有一个JSON文件:

(1)。成功返回json数据:

{
    "code": 100,
    "result": {
        "pic": "http%3A%2F%2Fres1.adyun.com%2Fres1%2Fupload %2F40543%2Fidea_201609%2F14736434292154.jpg",
        "clickurl": "http%3A%2F%2Fs.adyun.com%2Fsdkclick%3Fa%3D3062120448_3895541760_4%26i %3D5575715%26c

%3D7DA0B90B6DF81BD015A97357FB7F%26d%3D924737536_557985792_4246247424_6040307314919997440%26e

%3Dd4ee13b64d2cd3f7_2%26g %3D0%26h%3D329d7b4280da7ffc%26s%3D29281%26k%3D2%26j%3D5%26l%3D0%26v%3D3%26r 

%3D687474703A2F2F7777772E786C7A73736A2E636F6D2F",
        "holycrap": "557985792_4246247424_6040307314919997440",
        "telno": ""
    }
}

(2)失败返回json数据:

{
    "code":-23, 

    "result":"no ad"
}

2.进入对象列表。

BaseRet baseRet = new Gson().fromJson(content, BaseRet.class);

3.这是我正在使用的对象类。

BaseRet:

public class BaseRet {

    public int code;//": 100

    public AdData result;//<-
}

ADDATA:

public class AdData {
    public String pic;           
    public String clickurl;      
    public String clicknotifyurl;

    public String holycrap;      
    public String telno;         
    public String getClicknotifyurl() {
         return clicknotifyurl;
    }


    public void setClicknotifyurl(String clicknotifyurl) {
        this.clicknotifyurl = clicknotifyurl;
    }

    public String getClickurl() {
        return clickurl;
    }

    public void setClickurl(String clickurl) {
        this.clickurl = clickurl;
    }

    public String getHolycrap() {
        return holycrap;
    }

    public void setHolycrap(String holycrap) {
        this.holycrap = holycrap;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }

    public String getTelno() {
        return telno;
    }

    public void setTelno(String telno) {
        this.telno = telno;
    }
}

4.但它抛出了我

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 24 path $.result

5.任何想法我该如何解决?

1 个答案:

答案 0 :(得分:1)

关键是你的结果失败响应是STRING,而不是JSONObject。 如果你想修复,请不要使用一个键来表示不同的类型。

e.g。

&#13;
&#13;
class BaseRet<T>{
     int code;
     String msg;
     T result;
}
&#13;
&#13;
&#13;

移动失败结果&#39;没有广告&#39; to param msg,let result为null