首先聲明一個class類:
public class codestr
{
public int code { get; set; }
public bool success { get; set; }
public string message { get; set; }
public string data { get; set; }
}
直接使用對象:
引用 using Newtonsoft.Json;
public void mail()
{
codestr code = new codestr();
code =FromJSON<codestr>("{"code":200,"success ":true,"message ":null,"data ":null}"); //字符串轉實例 格式以大括號{}加入
}
#region 字符串轉對象list
public static T FromJSON<T>(string input)
{
try
{
return JsonConvert.DeserializeObject<T>(input);
}
catch (Exception ex)
{
return default(T);
}
}
#endregion