-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!

1
  • 3
    Have you tried public setters and not declaring a constructor? Commented Feb 3, 2018 at 16:00

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

this worked, thank you too
@Xabi welcome, :)
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() { }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.