JSON说null引用,但计数是0
我想从这段代码中取回数值。 一切看起来对我来说都很好,反序列化不会给出任何错误,所以我假设它在那里都可以。JSON说null引用,但计数是0
如果我对var进行计数,我会返回一个值。
这里是我的JSON:
{ "Activity": [
{
"ActivityLocation": {
"Address": {
"City": "LA",
"StateProvinceCode": "CA",
"CountryCode": "US"
}
},
"Status": {
"Type": "I",
"Description": "Departure Scan",
"Code": "DP"
},
"Date": "20171201",
"Time": "033400"
},
{
"ActivityLocation": {
"Address": {
"City": "LA",
"StateProvinceCode": "CA",
"CountryCode": "US"
}
},
"Status": {
"Type": "I",
"Description": "Origin Scan",
"Code": "OR"
},
"Date": "20171130",
"Time": "185600"
},
{
"ActivityLocation": {
"Address": {
"CountryCode": "US"
}
},
"Status": {
"Type": "M",
"Description": "Order Processed: Ready for UPS",
"Code": "MP"
},
"Date": "20171129",
"Time": "193858"
}
],}
这里是我的课:
public class Address {
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string CountryCode { get; set; }
}
public class ActivityLocation
{
public Address Address { get; set; }
}
public class Status
{
public string Type { get; set; }
public string Description { get; set; }
public string Code { get; set; }
}
public class Activity
{
public ActivityLocation ActivityLocation { get; set; }
public Status Status { get; set; }
public string Date { get; set; }
public string Time { get; set; }
}
public class RootObject
{
public List<Activity> Activity { get; set; }
}
var huh = JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
MessageBox.Show(huh[0].Activity[0].ActivityLocation.Address.City);
我希望得到回城的字符串值
我得到空引用错误。
确切的错误是:
System.NullReferenceException:对象不设置为一个对象的一个实例。在UPSTracking.UPSTrack。 <> c_DisplayClass1.b _()(IRestResponse响应)
回答:
您有基于我立刻看到了你的JSON有效载荷,你有{ Activity: [.....
所以你需要在你的代码更改为以下
var huh = JsonConvert.DeserializeObject<List<Activity>>(jsonString);
以上是 JSON说null引用,但计数是0 的全部内容, 来源链接: utcz.com/qa/258821.html