Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Json.Net是可为null的<;布尔>;论反序列化_C#_Asp.net Mvc_Json.net_Asp.net Web Api - Fatal编程技术网

C# Json.Net是可为null的<;布尔>;论反序列化

C# Json.Net是可为null的<;布尔>;论反序列化,c#,asp.net-mvc,json.net,asp.net-web-api,C#,Asp.net Mvc,Json.net,Asp.net Web Api,我已经设置了一个WebAPI控制器,它返回的数据集在SQL数据库上有一个可为空的布尔值(can_personalize) 当我直接下载.json文件时,布尔属性包含数据(true/false)。当我使用Newtonsoft JsonConvert.DeserializeObject()时,每个对象的can_personalize属性值都为NULL 我在其他几个类似的问题回答中尝试了一些建议,但没有一个能将问题分类 JSON示例: { "$id": "1", "$values": [

我已经设置了一个WebAPI控制器,它返回的数据集在SQL数据库上有一个可为空的布尔值(can_personalize)

当我直接下载.json文件时,布尔属性包含数据(true/false)。当我使用Newtonsoft JsonConvert.DeserializeObject()时,每个对象的can_personalize属性值都为NULL

我在其他几个类似的问题回答中尝试了一些建议,但没有一个能将问题分类

JSON示例:

{
  "$id": "1",
  "$values": [
    {
      "$id": "2",
      "TagTypeID": 1,
      "tagIndex": 0,
      "TagCategoryId": 1,
      "validtagtypeid": 0,
      "PrimaryTagTypeValue": "MC",
      "PrimaryDescription": "Motorcycle - Standard",
      "TagCategory": "StandardPlate",
      "SecondaryTagTypeValue": "02",
      "SecondaryDescription": "Personalized",
      "TagEndDate": "9999-12-31T00:00:00",
      "PrimaryEndDate": "9999-12-31T00:00:00",
      "SecondaryEndDate": "12/31/9999",
      "ValidRegex": "^[A-Z\\d -]{1,7}$",
      "primarytagtypeid": 78,
      "secondarytagtypeid": 2,
      "can_personalize": true,
      "ImageName": "redacted"
    }
  ]
}
由ADO.NET模型生成的类:

public partial class ttGetTagListWithCategory_Result
{
    public long TagTypeID { get; set; }
    public int tagIndex { get; set; }
    public Nullable<int> TagCategoryId { get; set; }
    public int validtagtypeid { get; set; }
    public string PrimaryTagTypeValue { get; set; }
    public string PrimaryDescription { get; set; }
    public string TagCategory { get; set; }
    public string SecondaryTagTypeValue { get; set; }
    public string SecondaryDescription { get; set; }
    public Nullable<System.DateTime> TagEndDate { get; set; }
    public Nullable<System.DateTime> PrimaryEndDate { get; set; }
    public string SecondaryEndDate { get; set; }
    public string ValidRegex { get; set; }
    public Nullable<int> primarytagtypeid { get; set; }
    public Nullable<int> secondarytagtypeid { get; set; }
    public bool can_personalize { get; set; }
    public string ImageName { get; set; }
}
公共部分类ttGetTagListWithCategory\u结果
{
公共长TagTypeID{get;set;}
公共int标记索引{get;set;}
公共可空TagCategoryId{get;set;}
public int validtagtypeid{get;set;}
公共字符串PrimaryTagTypeValue{get;set;}
公共字符串PrimaryDescription{get;set;}
公共字符串TagCategory{get;set;}
公共字符串SecondaryTagTypeValue{get;set;}
公共字符串次要描述{get;set;}
公共可为空的标记日期{get;set;}
公共可为空的PrimaryEndDate{get;set;}
公共字符串SecondaryEndDate{get;set;}
公共字符串ValidRegex{get;set;}
公共可为空的primarytagtypeid{get;set;}
公共可空的secondarytagtypeid{get;set;}
公共bool可以对{get;set;}进行个性化设置
公共字符串ImageName{get;set;}
}
JsonConvert调用:

List<ttGetTagTypeListWithCategory_Result> tagTypeWithCategory =
            JsonConvert.DeserializeObject<List<ttGetTagTypeListWithCategory_Result>>
            (client.DownloadString(WebApiUtility.GetTagTypeListWithCategoryUri()));
List tagTypeWithCategory=
JsonConvert.DeserializeObject
(client.DownloadString(webapiputility.GetTagTypeListWithCategoryUri());

如果我在结果集类中添加[JsonProperty(PropertyName=“can\u personalize”)]数据注释,数据将加载到对象中。

能否显示您尝试反序列化的字符串、反序列化到的类,以及预期和实际输出?我将其添加到原始帖子中,我还注意到can_personalize在类上不能为空。。。HMMM您可以添加对DeserializeObject的调用吗?如果这是传递给
DeserializeObject
的实际json,那么问题在于您的集合是嵌套的。您没有将表示集合的部分传递给它,而是将包含集合的父对象传递给它。