无法将字符串解析为我的对象

时间:2017-12-28 22:23:08

标签: java json

这是我收到的字符串是jsonlint的验证json     String decodingCookie = URLDecoder.decode(myCookie.getValue(),“UTF-8”);                 Gson g = new Gson();

这里的字符串是什么样的

{
    "urlAW": "http://www.google.com",
    "urlApi": "http://www.google.com",
    "lang": "en",
    "infoComplete": "{\"id\":\"569\",\"status\":\"START\"}",
    "isApp": false,
    "isActive": false
}

Infos = g.fromJson(decodedCookie, Infos.class);

每次我尝试解析时, infoComplete

都会失败
  

给出错误java.lang.IllegalStateException:预期BEGIN_OBJECT   但STRING在第1行

但InfoComplete.class只有2个字段 id 状态,包含getter和setter

不确定我错过了什么才能让它发挥作用。任何帮助将不胜感激。

Infos类看起来像这样

public class Infos {
    private String urlAW;
    private String urlApi;   
    private String lang;
    private InfoComplete infoComplete;
    private boolean isApp;
    private boolean isActive;

    public String getUrlAW() {
        return urlAW;
    }

    public void setUrlAW(String urlAW) {
        this.urlAW = urlAW;
    }

    public String getUrlApi() {
        return urlApi;
    }

    public void setUrlApi(String urlApi) {
        this.urlApi = urlApi;
    }

    public String getLang() {
        return langue;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public InfoComplete getInfoComplete() {
        return infoComplete;
    }

    public void setInfoComplete(InfoComplete infoComplete) {
        this.infoComplete = infoComplete;
    }

    public boolean isApp() {
        return isApp;
    }

    public void setApp(boolean app) {
        isApp = app;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
    }


    @Override
    public String toString() {
        return "Infos{" +
                "urlAW='" + urlAW + '\'' +
                ", urlApi='" + urlApi + '\'' +
                ", lang='" + lang + '\'' +
                ", infoComplete='" + infoComplete + '\'' +
                ", isApp=" + isApp +
                ", isActive=" + isActive +
                '}';
    }
}

1 个答案:

答案 0 :(得分:0)

此处的问题是infoComplete的类型

"infoComplete": "{\"id\":\"569\",\"status\":\"START\"}"

尝试检查响应,或者可以将该字符串转换为有效的JSONObject:

{
  "urlAW": "http://www.google.com",
  "urlApi": "http://www.google.com",
  "lang": "en",
  "infoComplete": {
    "id": "569",
    "status": "START"
  },
  "isApp": false,
  "isActive": false
}