Reputation: 16128
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?
Upvotes: 2
Views: 3951
Reputation: 950
Just trim out the quotes: authorization = authorization.substring(1, authorization.length() - 1)
Edit: You should actually use the getString()
method as it will return the actual content as a string, however, the above still works as a quick workaround.
Upvotes: 1
Reputation: 9009
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
Upvotes: 10
Reputation: 1138
You are getting Object from Json and then converting to String. jobject.get("authorization").toString();
try to get String.
String authorization = jobject.getString("authorization");
Upvotes: 4