實體類:
public class CheckData
{
public string msg;
public string code;
public string data;
public string canPublish;
public string userName;
public string loginName;
public string userRole;
}
轉換方法:
/// <summary>
/// 從一個Json串生成對象信息
/// </summary>
/// <param name="jsonString">JSON字符串</param>
/// <typeparam name="T">對象類型</typeparam>
/// <returns></returns>
public static T ToObject<T>(string json) where T : class
{
if (String.IsNullOrEmpty(json)) return null;
T obj = JsonConvert.DeserializeObject<T>(json);
return obj;
}
調用:
var sRemoteInfo = "{'canPublish':'true','msg': '登錄成功 ','code': 1,'data': 'tokenstring','userName': 'user1','loginName': 'user1' }";
var userInfo = JsonHelper.ToObject<CheckData>(sRemoteInfo);
