从http response.body()获取Json字符串

时间:2017-08-22 10:26:25

标签: java json httpresponse

我想知道是否有办法从http response.body()中提取Json字符串。我的response.body()里面有{"er":"manualBlock"},我想处理这个字符串而不必使用split方法。

编辑 到目前为止我有这个:

String[] parts = response.body().string().split("-"); 
        result = parts[0]; 

if (result != null && result.equals("{\"er\":\"manualBlock\"}")) {
          throw new BlockeduserException("User blocked", null);
        }

1 个答案:

答案 0 :(得分:1)

我设法通过创建这样的类来解决我的问题:

public class BlockResponse {

    public String er;
}

然后我使用google-Gson来处理所有事情:

String serverResponse = response.body().string();
Gson gson = new Gson();
result = gson.fromJson(serverResponse, BlockResponse.class);

对于我使用的比较:

if (result != null && result.er.equals("manualBlock")) {
    throw new BlockeduserException("User blocked", null);
}