本文介绍了Jsonconvert.deserialize到类返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么jsoncovert反序列化会返回null?
Why does jsoncovert deserialization return null?
string strjson = "{\"testinfo\":" + "[{\"testid\":1,\"testshortdescription\":\"tanktest\",\"testlongdescription\":\"Tank Test \",\"testtypeid\":2,\"testlimitsid\":null}," + "{\"testid\":2,\"testshortdescription\":\"FuelPump1\",\"testlongdescription\":\"Fuel Pump 1\",\"testtypeid\":4,\"testlimitsid\":null}," + "{\"testid\":3,\"testshortdescription\":\"FuelPump2\",\"testlongdescription\":\"Fuel Pump 2\",\"testtypeid\":4,\"testlimitsid\":null}," + "{\"testid\":4,\"testshortdescription\":\"TankAudit\",\"testlongdescription\":\"Tank Chamber Audit\",\"testtypeid\":6,\"testlimitsid\":null}]}";
TestInfoCollectionCS testinfocollections = JsonConvert.DeserializeObject<TestInfoCollectionCS>(strjson);
这是TestCS类:
Here is the TestCS class:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestITMobileApp.Models
{
public class TestCS
{
#region "Private variables""
private int TestID;
private string TestShortDescription;
private string TestLongDescription;
private int? TestTypeID;
private int? TestLimitsID;
#endregion
#region "Properties"
#region "testid"
public int testid { get => TestID; set => TestID = value; }
#endregion
#region "testshortdescription"
public string testshortdescription { get => TestShortDescription; set => TestShortDescription = value; }
#endregion
#region "testlongdescription"
public string testlongdescription { get => TestLongDescription; set => TestLongDescription = value; }
#endregion
#region "testtypeid"
public int? testtypeid { get => TestTypeID; set => TestTypeID = value; }
#endregion
#region "testlimitsid"
public int? testlimitsid { get => TestLimitsID; set => TestLimitsID = value; }
#endregion
#endregion
}
}
包含列表类的TestInfoCollectionCS类:
The class TestInfoCollectionCS that contains the list class:
using System;
using System.Collections.Generic;
using System.Text;
using TestITMobileApp.Model;
using TestITMobileApp.Models;
namespace TestITMobileApp.App_Data
{
public class TestInfoCollectionCS
{
private List<TestCS> testinfocollection;
public List<TestCS> TestInfoCollection { get => testinfocollection; set => testinfocollection = value; }
}
}
我尝试了什么:
反序列化返回null?
What I have tried:
Deserialization returning null?
推荐答案
public class Testinfo
{
public int testid { get; set; }
public string testshortdescription { get; set; }
public string testlongdescription { get; set; }
public int testtypeid { get; set; }
public object testlimitsid { get; set; }
}
public class RootObject
{
public List<Testinfo> testinfo { get; set; }
}
而不是TestCS。
如果我尝试使用这些类,它会反序列化为单个列表中的实例:
Instead of TestCS.
If I try with those classes, it deserializes fine as a single instance in the list:
string strjson = "{\"testinfo\":" + "[{\"testid\":1,\"testshortdescription\":\"tanktest\",\"testlongdescription\":\"Tank Test \",\"testtypeid\":2,\"testlimitsid\":null}," + "{\"testid\":2,\"testshortdescription\":\"FuelPump1\",\"testlongdescription\":\"Fuel Pump 1\",\"testtypeid\":4,\"testlimitsid\":null}," + "{\"testid\":3,\"testshortdescription\":\"FuelPump2\",\"testlongdescription\":\"Fuel Pump 2\",\"testtypeid\":4,\"testlimitsid\":null}," + "{\"testid\":4,\"testshortdescription\":\"TankAudit\",\"testlongdescription\":\"Tank Chamber Audit\",\"testtypeid\":6,\"testlimitsid\":null}]}";
Console.WriteLine("{0}", strjson);
RootObject x = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(strjson);
Testinfo ti = x.testinfo[0];
这篇关于Jsonconvert.deserialize到类返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!