首先通过一定的方法得到String类型的Json数据。
然后利用fastjson中的JSON的parseObject()将String解析为JSONObject
然后通过get方法得到某个键对应的值:
private String getOpenid(String code) {
Map<String, String> map = new HashMap<>();
map.put("appid", weChatProperties.getAppid());
map.put("secret", weChatProperties.getSecret());
map.put("js_code", code);
map.put("grant_type", "authorization_code");
String json = HttpClientUtil.doGet(WX_LOGIN, map); // 获取到的json返回数据的String格式
// 使用fastjson中的JSON的parseObject()将String解析为JSONObject
JSONObject jsonObject = JSON.parseObject(json); // 得到JsonObject格式的数据
String openid = jsonObject.getString("openid"); // 使用getString方式获取到该key对应的value
return openid;
}