考虑以下教程:http: //blogs.msdn.com/b/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx
{
"firstName" : "Rakki",
"lastName" : "Muthukumar",
"department" : "Microsoft PSS",
"address" : {
"addressline1" : "Microsoft India GTSC",
"addressline2" : "PSS - DSI",
"city" : "Bangalore",
"state" : "Karnataka",
"country" : "India",
"pin" : 560028
},
"technologies" : ["IIS", "ASP.NET", "JavaScript", "AJAX"]
}
对于 json 代码,我有以下类:
public class Address
{
public string addressline1 { get; set; }
public string addressline2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public int pin { get; set; }
}
public class RootObject
{
public string firstName { get; set; }
public string lastName { get; set; }
public string department { get; set; }
public Address address { get; set; }
public List<string> technologies { get; set; }
}
当我尝试设置数据时,上述属性给了我错误
RootObjectClsObject.address.addressline1 = "NO";
它把我扔了NullReferrenceException
。如果我修改该行
public List<string> technologies { get;set;}
通过以下行
public List<string> technologies = new List<string>();
我不想使用它。因为我有复杂类型的 JSON 无法以这种方式处理。