我有一个包含json格式信息的字符串。这是一个oauth令牌。
我想提取refresh_token
。我该怎么做?
这是从inputLine打印的json:
{
{ "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);
}
答案 0 :(得分:1)
考虑这是您的json对象
{
"access_token":"ffa6ea48612b19966383fd6b134fe2c4daac80ce31a8632284bb7f9ad796b8 9a",
"expires_in":"1800000",
"token_type":"Bearer",
"scope":"issuer serial",
"refresh_token":"035075054a454d1257281f89c4ce646f40f0ee935dbf19201a03976cd8890739"
}
这是答案:
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
答案 1 :(得分:-1)
您的Json响应无效,因为它具有重复的密钥。如果您无法控制响应。您可以尝试这种解决方法。
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);
答案 2 :(得分:-1)
我用json简单的问题解决了-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);
这是解决问题的代码。我在这里发布,也许有人需要它