-3

I'm trying to convert a json array of objects to a C# list, but I can't make it work. Currently, I have made this class:

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() { }
}

Now, I have this JSON that i want to deserialize, which seems to be correctly formed:

[  
   {  
      "officer":"Alessia Smith",
      "target":"Scott Turner",
      "amount":1800,
      "reason":"test",
      "date":"9/4/2017 3:32:04 AM"
   }
]

And the C# line which should do the magic is:

List<FineModel> removedFines = JsonConvert.DeserializeObject<List<FineModel>>(json);

It returns one object, but when I try to print its values, it returns 0 for the amount property and empty for the strings, like if i did . What could be wrong here?

Thanks in advance!

programtreasures
  • 4,250
  • 1
  • 10
  • 29
Xabi
  • 159
  • 1
  • 3
  • 13

2 Answers2

3

Remove internal from setter,

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; }
}

the internal setter will not work because called from another dll

programtreasures
  • 4,250
  • 1
  • 10
  • 29
2

Just to make answers more complete, Either remove the internal from setters or add JsonProperty attributes to your model.

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() { }
}
EZI
  • 15,209
  • 2
  • 27
  • 33