我收到来自 API 的响应,该响应生成带有其他一些属性的授权 token ,我正在尝试从响应中提取 token 的值,如下所示
{"access_token":"tokenvalue","scope":"somevalue","token_type":"Bearer","expires_in":value}
我尝试了如下 JSON 解析:
Myclass response = template.postForObject(url, requestEntity, Myclas.class);
JSONParser jsonParser = new JSONParser();
JSONObject obj = (JSONObject) jsonParser.parse(response);
String product = (String) jsonObject.get("access_token");
token = response;
}
收到错误:
parse( )
in JSONParser cannot be applied
to
(java.lang.String)
最佳答案
用行:
String product = (String) jsonObject.get("access_token");
您正在尝试获取名为“access_token”的属性。
但是,如果您查看 json,您会发现没有名为“access_token”的属性。
也许你的意思是:
String product = (String) jsonObject.get("token");
关于java - java解析JSON时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50216707/