0

I have a String that contains a json format information. It is an oauth token. I would like to extract the refresh_token. How can I do it?

This is the json that is printing from inputLine:

  {
    {   "access_token":"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b8 
   9a",
 "access_token":"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b8 
   9a",
   "expires_in":"1800000",
   "expires_in":"1800000",
   "token_type":"Bearer",
   "token_type":"Bearer",
   "scope":"issuer serial",
   "scope":"issuer serial",

 "refresh_token":"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739"
  "refresh_token":"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739"
}
}

 while ((inputLine = in2.readLine()) != null) {
                System.out.println(inputLine);
                Gson gs = new Gson();
                RefreshToken rt = new RefreshToken();
                rt.t = inputLine;
                String xjson = gs.toJson(rt.t);

                System.out.println(xjson);



            }
jsheeran
  • 2,912
  • 2
  • 17
  • 32
  • 1
    [https://stackoverflow.com/questions/20152710/gson-get-json-value-from-string](https://stackoverflow.com/questions/20152710/gson-get-json-value-from-string) – bkis Jun 04 '19 at 11:06
  • 1
    Is this a valid json? Why it has duplicate keys? Check your Json string here https://jsonlint.com/ – maddy23285 Jun 04 '19 at 11:07

3 Answers3

1

considering this is your json object

{
"access_token":"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b8 9a",
"expires_in":"1800000",
"token_type":"Bearer",
"scope":"issuer serial",
"refresh_token":"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739"
}

here is the answer:

Gson gson= new Gson();
String json= 
{\"access_token\":\"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b8 
9a\",\"expires_in\":\"1800000\",\"token_type\":\"Bearer\",\"scope\":\"issuer serial\"
,\"refresh_token\":\"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd889 
0739\"}";

Map map=gson.fromJson(json.toString(), Map.class);
System.out.println(map.get("refresh_token")); //output 
035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739
karthick S
  • 564
  • 1
  • 9
  • 18
-1

Your Json response is invalid as it has duplicate keys. If you don't have control over response. You can try this workaround.

String in2 = "{{\"access_token\":\"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b89a\", \"access_token\":\"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b89a\",\"expires_in\":\"1800000\",   \"expires_in\":\"1800000\",   \"token_type\":\"Bearer\",   \"token_type\":\"Bearer\",   \"scope\":\"issuer serial\",   \"scope\":\"issuer serial\", \"refresh_token\":\"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739\",  \"refresh_token\":\"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739\"}}";
in2 = in2.replaceFirst("access_token","access_token_1").replaceFirst("expires_in","expires_in_1").replaceFirst("token_type","token_type_1").replaceFirst("scope","scope_1").replaceFirst("refresh_token","refresh_token_1").substring(1);
in2 = in2.substring(0,in2.length()-1);
maddy23285
  • 738
  • 6
  • 16
-1

I solved the issue with json simple - http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm

               JSONParser parser = new JSONParser();
               JSONObject jsonObject = (JSONObject) parser.parse(ilt);
               String reftok = (String) jsonObject.get("refresh_token").toString();
               System.out.println("Refresh Token : ");
               System.out.println(reftok);

This is the code that solved the problem. I posted here maybe someone else needs it