Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android JsonObject parsing string with double quotes

Tags:

json

android

Im using JsonObject to parse a response from an API, the problem is that the resulting String have double quotes, how to get string with no quotes?

                JsonElement jelement = new JsonParser().parse(response);
                JsonObject jobject = jelement.getAsJsonObject();
                String authorization = jobject.get("authorization").toString();

                Log.d("mensa","parseado jwt :"+authorization);

so my response looks like...

parseado jwt :"eyJ0eXAiOiJKV1QiLCJhbGciO..."

Actual Json response

{
    "authorization": "eyJ0eXAiOiJKV1..."
}

what is the right way to parse this string so i dont get it wrapped with double quotes?

like image 281
manuelBetancurt Avatar asked Sep 16 '26 03:09

manuelBetancurt


2 Answers

I believe you are using GSON library. No need to trim out your result, gson provide methods for that. try this

 String authorization = jobject.get("authorization").getAsString();

Refer Gson API Doc : JsonElement

like image 53
Bharatesh Avatar answered Sep 17 '26 16:09

Bharatesh


You are getting Object from Json and then converting to String. jobject.get("authorization").toString(); try to get String.

String authorization = jobject.getString("authorization");
like image 23
Ahmad Nawaz Avatar answered Sep 17 '26 17:09

Ahmad Nawaz