[英]How to deserialize json object in C#
I am trying to deserialize the json string to ac# object. 我正在尝试将json字符串反序列化为ac#对象。
string str ="[{ \"foo\" : \"A\" , \"bar\" : \"B\"}, { \"foo\" : \"C\" , \"bar\" : \"D\"}]";
public Class Example
{
public string foo { get; set; }
public string bar { get; set; }
}
JavaScriptSerializer Js = new JavaScriptSerializer();
Example[] ex = (Example[]) Js.DeserializeObject(str);
But I am getting an InvalidCast Exception
. 但是我收到一个
InvalidCast Exception
。 What am I doing wrong? 我究竟做错了什么?
var list = new JavaScriptSerializer().Deserialize<List<FooBar>>(str);
public class FooBar
{
public string foo;
public string bar;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.