简体   繁体   中英

Get Json string from http response.body()

I would like to know if there's any way to extract a Json string from a http response.body() . Inside my response.body() I have: {"er":"manualBlock"} and I would like to deal with this string without having to use the split method.

Edit I have this so far:

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

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

I managed to solve my problem by creating a class like this:

public class BlockResponse {

    public String er;
}

And then I used the google-Gson to handle everything by doing this:

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

And for the comparison I used:

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

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