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

Get Json string from http response.body()

我想知道是否有任何方法可以从 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);
        }

我设法通过创建这样的 class 解决了我的问题:

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