"{\"access_token\":\"5RtqZYttHHb3hoLfvayvkV6yFabyb67Re3KFThqsuHHy1DbjB6d3-Ras4jz0at2b20FoiJIhSFc5lS6bbFrlbjt9eX1JZareUh98TBJFOnI\",\"expires_in\":7200,\"refresh_token\":\"5KNZMgScv54mk9-jqidBLIM26epyRSk2oxq4zwDlgkEBBJVtZD2A6cWUH_j8zuVWPVaVLOZtW5N9VjXpnVJQKWqpvN-yYrggQcfguE6lfu0\",\"openid\":\"ok8M3wez1xhN8bER2QLLRUWkbbqA\",\"scope\":\"snsapi_userinfo\"}",
比如这个json数据,在java中怎么获得其中openid的值
反序列化到一个实体对象中。这个对象的属性openid的值就是你要的。
怎样反序列化?能给代码吗?
1 string jsonStr="{\"access_token\":\"5RtqZYttHHb3hoLfvayvkV6yFabyb67Re3KFThqsuHHy1DbjB6d3-Ras4jz0at2b20FoiJIhSFc5lS6bbFrlbjt9eX1JZareUh98TBJFOnI\",\"expires_in\":7200,\"refresh_token\":\"5KNZMgScv54mk9-jqidBLIM26epyRSk2oxq4zwDlgkEBBJVtZD2A6cWUH_j8zuVWPVaVLOZtW5N9VjXpnVJQKWqpvN-yYrggQcfguE6lfu0\",\"openid\":\"ok8M3wez1xhN8bER2QLLRUWkbbqA\",\"scope\":\"snsapi_userinfo\"}"; 2 3 JavaScriptSerializer js = new JavaScriptSerializer(); 4 Info info = (Info)js.Deserialize(jsonStr, typeof(Info)); 8 } 9 public class Info 10 { 11 public string OpenId { get; set; } 12 } 13 14 }
这是C#代码,Java的话你可以照葫芦画瓢
1 String jsonStr="{\"access_token\":\"5RtqZYttHHb3hoLfvayvkV6yFabyb67Re3KFThqsuHHy1DbjB6d3-Ras4jz0at2b20FoiJIhSFc5lS6bbFrlbjt9eX1JZareUh98TBJFOnI\",\"expires_in\":7200,\"refresh_token\":\"5KNZMgScv54mk9-jqidBLIM26epyRSk2oxq4zwDlgkEBBJVtZD2A6cWUH_j8zuVWPVaVLOZtW5N9VjXpnVJQKWqpvN-yYrggQcfguE6lfu0\",\"openid\":\"ok8M3wez1xhN8bER2QLLRUWkbbqA\",\"scope\":\"snsapi_userinfo\"}"; 2 JSONObject jsonObj=new JSONObject(jsonStr); 3 String result=jsonObj.getString("openid"); 4 System.out.println(result);
我这边用的是java-json.jar