首页 新闻 会员 周边

json转实体类帮忙看看哦.net

0
悬赏园豆:20 [已解决问题] 解决于 2015-04-15 17:27

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; }
}
}

mkj小陈的主页 mkj小陈 | 初学一级 | 园豆:6
提问于:2015-04-15 15:52
< >
分享
最佳答案
0

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 += "}";

收获园豆:20
Yu | 专家六级 |园豆:12990 | 2015-04-15 17:22

哪个属性啊,新手啊。

mkj小陈 | 园豆:6 (初学一级) | 2015-04-15 17:22

@mkj小陈: 全部

Yu | 园豆:12990 (专家六级) | 2015-04-15 17:23

@Yu: 看懂了,我去试试 谢谢你哦。

mkj小陈 | 园豆:6 (初学一级) | 2015-04-15 17:24

@Yu: 还真区分大小写,解决了,谢谢你 哦

mkj小陈 | 园豆:6 (初学一级) | 2015-04-15 17:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册