2016-12-30 256 views
0

我使用GSON解析一個JSON字符串像這樣的:致:java.lang.IllegalStateException:預期字符串但BEGIN_OBJECT

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

當我用下面的代碼一切工作正常:

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; 
    } 
} 

但是,當我使用下面的代碼的東西不太工作:

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

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

我得到這個異常:

致:java.lang.IllegalStateException:預期字符串但BEGIN_OBJECT在3號線列24路$ .showapi_res_body

我怎樣才能讓使用GSON這項工作?

+2

的可能的複製[GSON:預計字符串但BEGIN \ _object(http://stackoverflow.com/questions/11571412/gson-expected-a-string-but-was-begin-object ) –

+0

謝謝,我來看看 – xujun

回答

1

添加單獨的類不是內部類

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

public class Body{ 
     int totalNum; 
     int ret_code; 
    } 

或者

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; 
} 
} 

要獲得詳細

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"); 
+0

我按照你的建議, HashMap stringMap = map.showapi_res_body; String string = stringMap.get(「showapi_res_body」); 但結果是這樣的, > [Ljava.lang.String; @ 1de0aca6 不是字符串,我應該怎麼得到stirng > { 「totalNum」:44, 「ret_code」 :0 } – xujun

+0

好吧,我來看看 – xujun

+0

我按照你的建議, 'HashMap stringMap = map.showapi_res_body; String string = stringMap.get(「showapi_res_body」);' 但結果是這樣的, '[Ljava.lang。字符串; @ 1de0aca6' 不是字符串,我應該怎麼得到stirng '{ 「totalNum」:44, 「ret_code」:0 }' – xujun

0

showapi_res_body不是String值。在你之前的例子中,你使用了一個Body對象,我建議你使用相同的對象。如果你想輸出String,嘗試做手工,如:

public static class Body { 

    int totalNum; 
    int ret_code; 

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

然後,您可以撥打:

String output = bean1.showapi_res_body.toString(); 
+0

Terlingen,如果我這樣使用,字符串的結果不是json。你有更好的辦法嗎? – xujun

0

嗯,我想這是顯而易見的,如果你希望它是字符串,你JSON應該具有字段類型的字段。

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

你回答好,謝謝 – xujun

相關問題