Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.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反序列化程序返回空对象列表_C#_Json_Deserialization - Fatal编程技术网

C#JSON反序列化程序返回空对象列表

C#JSON反序列化程序返回空对象列表,c#,json,deserialization,C#,Json,Deserialization,我试图将json对象数组转换为C#列表,但无法使其正常工作。目前,我已经完成了以下课程: public class FineModel { public String officer { get; internal set; } public String target { get; internal set; } public int amount { get; internal set; } public String reason { get; intern

我试图将json对象数组转换为C#列表,但无法使其正常工作。目前,我已经完成了以下课程:

public class FineModel
{
    public String officer { get; internal set; }
    public String target { get; internal set; }
    public int amount { get; internal set; }
    public String reason { get; internal set; }
    public String date { get; internal set; }

    public FineModel() { }
}
现在,我想反序列化这个JSON,它的格式似乎是正确的:

[  
   {  
      "officer":"Alessia Smith",
      "target":"Scott Turner",
      "amount":1800,
      "reason":"test",
      "date":"9/4/2017 3:32:04 AM"
   }
]
而C#线应该起到神奇的作用:

List<FineModel> removedFines = JsonConvert.DeserializeObject<List<FineModel>>(json);
List removedFines=JsonConvert.DeserializeObject(json);
它返回一个对象,但当我尝试打印它的值时,它为amount属性返回0,为字符串返回空,就像我那样。这里可能出了什么问题


提前谢谢

从设定器上拆下内部

public class RootObject
{
    public string officer { get; set; }
    public string target { get; set; }
    public int amount { get; set; }
    public string reason { get; set; }
    public string date { get; set; }
}

内部setter将不起作用,因为从另一个dll调用它只是为了使答案更完整,可以从setter中删除内部setter,或者将JsonProperty属性添加到模型中

public class FineModel
{
    [JsonProperty]
    public String officer { get; internal set; }
    [JsonProperty]
    public String target { get; internal set; }
    [JsonProperty]
    public int amount { get; internal set; }
    [JsonProperty]
    public String reason { get; internal set; }
    [JsonProperty]
    public String date { get; internal set; }

    public FineModel() { }
}

您是否尝试过公共setter而不声明构造函数?这很有效,谢谢too@Xabi欢迎,:)