json字符串转换成对象问题
转换方法:
private T JsonToObject<T>(string jsonStr)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
private void Form1_Load(object sender, EventArgs e)
{
string jsonString1 = @"{""Description"":""this is a test"",""Enable"":""false"",""Title"":""test""}";
TestClass t1 = JsonToObject<TestClass>(jsonString1);
}
private T JsonToObject<T>(string jsonStr)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
public class TestClass
{
public string Title { get; set; }
public string Description { get; set; }
public bool Enable { get; set; }
}