如何使用json parese这个内容

时间:2015-06-04 07:18:42

标签: json gson

我得到了像这样的json字符串,

{
    "resultCode": 200,
    "operation": 0,
    "resultMsg": "正常初始化",
    "debugMsg": "",
    "resultCount": 3,
    "result": {
        "operation": 0,
        "verify_key": "6f3e9169e4fcfbe4a52606c013348650",
        "user_id": 41201
    }
}

但我想把它转换成,

public class ClientResult {
    String result;
    String resultMsg;
    String debugMsg;
    int resultCode;
    int operation;
}

。如果我使用gson.fromJson(result,ClientResult.class),得到一个异常

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期字符串但在第1行第118列是BEGIN_OBJECT

我只是想将它解析为String而不是Object,因为ClientResut的结果字段不相同,有时它可能是一个数组。 结果有时像



 {
   "resultCode": 200,
   "operation": 0,
   "resultMsg": "",
   "debugMsg": "",
   "resultCount": 12,
   "result": {
     "id": "4392",
     "pm25": "44",
     "time": "1433403001",
     "l_temperature": "9",
     "h_temperature": "20",
     "c_temperature": "14",
     "weather_icon": "http://60.31.215.212/zsgj/Public/weather/zhenyu.png",
     "tm_l_temperature": "8",
     "tm_h_temperature": "25",
   }
 }




所以我只想将它解析为String ..

1 个答案:

答案 0 :(得分:0)

因为你做错了,"结果"你的json中是另一个json对象,你可以为你的JON创建2个POJO,你可以在你的GSON中使用它。

public class Result
{
    private String operation;

    private String verify_key;

    private String user_id;

}

和原来的课程

public class ClientResult
{

    private String resultMsg;

    private Result result;

    private String debugMsg;

    private String operation;

    private String resultCount;

private String resultCode;

}