1.在后台获取openid是返回的是一个json格式的Object对象,如何获得里面的openid呢?
先把object对象转换成String格式,再转换成map集合,然后遍历集合获取。
obj的数据格式:openid+{“session_key”:“1KugMkenq***************==”,“openid”:“o**j-49ENOXbeIaBk**********”}
Object obj= restTemplate.getForObject("https://api.weixin.qq.com/sns/jscode2session?appid=自己的AppID&secret=自己的秘钥&js_code=" +code + "&grant_type=authorization_code",String.class);
//转换成String
String object1 = obj.toString();
JSONObject json=new JSONObject(object1);
Map<String,Object> map=new HashMap<String, Object>();
Iterator it = json.keys();
while (it.hasNext()) {
String key = (String) it.next();
Object value = json.get(key);
map.put(key, value);
}
String openid = (String) map.get("openid");