简体   繁体   中英

Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT

I'm using Gson to parse a JSON strings like this one:

{"showapi_res_code": 0,
  "showapi_res_error": "1",
  "showapi_res_body": {
    "totalNum": 44,
    "ret_code": 0
   }
 }

When I use the following code everything works fine:

Bean bean = gson.fromJson(stringFromSource, Bean.class);

public class Bean{
    int showapi_res_code;
    String showapi_res_error;
    Body showapi_res_body;

    public static class Body{
        int totalNum;
        int ret_code;
    }
}

But when I use the following code things don't quite work:

Bean1 bean1 = gson.fromJson(stringFromSource, Bean1.class);

public class Bean1 {
    int showapi_res_code;
    String showapi_res_error;
    String showapi_res_body;
}

I get this exception:

Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 3 column 24 path $.showapi_res_body

How can I make this work using Gson?

Add separate class not inner class

public class Bean{
    int showapi_res_code;
    String showapi_res_error;
    Body showapi_res_body;
} 

public class Body{
        int totalNum;
        int ret_code;
    }

Or

public class Bean{
        int showapi_res_code;
String showapi_res_error;
HashMap<String,Integer> showapi_res_body;

public int getShowapi_res_code() {
    return showapi_res_code;
}

public void setShowapi_res_code(int showapi_res_code) {
    this.showapi_res_code = showapi_res_code;
}

public String getShowapi_res_error() {
    return showapi_res_error;
}

public void setShowapi_res_error(String showapi_res_error) {
    this.showapi_res_error = showapi_res_error;
}

public HashMap<String, Integer> getShowapi_res_body() {
    return showapi_res_body;
}

public void setShowapi_res_body(HashMap<String, Integer> showapi_res_body) {
    this.showapi_res_body = showapi_res_body;
}
} 

To get the detail

Bean bean1 = gson.fromJson(stringFromSource, Bean1.class);
int totalNum = (Integer)bean1.getShowapi_res_body().get("totalNum");
int ret_code= (Integer)bean1.getShowapi_res_body().get("ret_code");

Well, I guess that's obvious, If you want it to be String, your JSON should have the field as a String type.

{"showapi_res_code": 0,
  "showapi_res_error": "1",
  "showapi_res_body": "{ \"totalNum\": 44, \"ret_code\": 0}"
 }

The showapi_res_body is not a String value. In your previous example you use a Body object, I suggest you to use the same. If you want to output a String , try to do it manually like:

public static class Body { 

    int totalNum;
    int ret_code; 

    @Override
    public String toString() {
        return "totalNum = " + totalNum + ", ret_code = " + ret_code;
    }
}

Then you can call:

String output = bean1.showapi_res_body.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM