How to convert a String to JsonObject using gson library

后端 未结 9 1269
既然无缘
既然无缘 2020-11-30 23:45

Please advice how to convert a String to JsonObject using gson library.

What I unsuccesfully do:

String stri         


        
9条回答
  •  时光取名叫无心
    2020-12-01 00:32

    Looks like the above answer did not answer the question completely.

    I think you are looking for something like below:

    class TransactionResponse {
    
       String Success, Message;
       List Response;
    
    }
    
    TransactionResponse = new Gson().fromJson(response, TransactionResponse.class);
    

    where my response is something like this:

    {"Success":false,"Message":"Invalid access token.","Response":null}
    

    As you can see, the variable name should be same as the Json string representation of the key in the key value pair. This will automatically convert your gson string to JsonObject.

提交回复
热议问题