string access_token = "OezXcEiiBSKSxW0eoylIeAlPynLvzQBIXIwFPJvGmrfokeXB1zeo7Pu5ME__Rrl8rr4NDz45xOjuGlTVlmN-coB3aFaWPmmUuR9nDEQzEIC120XNMfXuTXla_S2HNXQcQFL3s8Kk4Yz-iMeHIGf-NA";
string expires_in = "7200";
string refresh_token = "OezXcEiiBSKSxW0eoylIeAlPynLvzQBIXIwFPJvGmrfokeXB1zeo7Pu5ME__Rrl8nf1XD5fUVtOQxPiia8lZ-vYxPqsziQnErfX9NBLVrv36k9QHlFcF_FKNIDGKlarjEURdpIaM_StuRv3t4IFPhg";
string openid = "oDO0buG9yQdGDWxUt5qMSL6IallA";
string scope = "snsapi_userinfo";
string json = "{";
json += string.Format("\"access_token\":\"{0}\",\"expires_in\":\"{1}\",\"refresh_token\":\"{2}\",\"openid\":\"{3}\",\"scope\":\"{4}\"", access_token, expires_in, refresh_token, openid, scope);
json += "}";
OAuth_Token tokeInof = ParseFromJson<OAuth_Token>(json);
/// <summary>
/// 获取Json的Model
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="szJson"></param>
/// <returns></returns>
public static T ParseFromJson<T>(string szJson)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
return (T)serializer.ReadObject(ms);
}
}
/// <summary>
/// 获取相关信息
/// </summary>
public class OAuth_Token
{
/// <summary>
/// 获取相关信息
/// </summary>
public OAuth_Token()
{ }
/// <summary>
/// 授权access_token
/// </summary>
private string access_token = "";
/// <summary>
/// 授权access_token
/// </summary>
public string Access_token
{
get { return access_token; }
set { access_token = value; }
}
/// <summary>
/// 授权有效期
/// </summary>
private string expires_in = "";
/// <summary>
/// 授权有效期
/// </summary>
public string Expires_in
{
get { return expires_in; }
set { expires_in = value; }
}
/// <summary>
/// 刷新access_token
/// </summary>
private string refresh_token = "";
/// <summary>
/// 刷新access_token
/// </summary>
public string Refresh_token
{
get { return refresh_token; }
set { refresh_token = value; }
}
/// <summary>
/// 微信openid
/// </summary>
private string openid = "";
/// <summary>
/// 微信openid
/// </summary>
public string Openid
{
get { return openid; }
set { openid = value; }
}
/// <summary>
/// 用户授权的作用域,使用逗号(,)分隔
/// </summary>
private string scope = "";
/// <summary>
/// 用户授权的作用域,使用逗号(,)分隔
/// </summary>
public string Scope
{
get { return scope; }
set { scope = value; }
}
}
json 与 model 的属性 大小写要一致
string json = "{"; json += string.Format("\"Access_token\":\"{0}\",\"Expires_in\":\"{1}\",\"Refresh_token\":\"{2}\",\"Openid\":\"{3}\",\"Scope\":\"{4}\"", access_token, expires_in, refresh_token, openid, scope); json += "}";
哪个属性啊,新手啊。
@mkj小陈: 全部
@Yu: 看懂了,我去试试 谢谢你哦。
@Yu: 还真区分大小写,解决了,谢谢你 哦