anotheruser
anotheruser

Reputation: 51

IList to JSON fails

I had this working before and now its consistently failing to convert my IList to a JSON in a AsP Core web app. I tried NewtonJson Lib, native Json() and Ok() none works...now. I see the returned json always as [{},{},{},{},{}] now.

I tried these already on my aspcore web app.

result = Content(JsonConvert.SerializeObject(status), "application/json");
result = Json(status);
result = Ok(status);

sample console app below for simplicity:

        IList<Sate> c = new List<Sate>();
        c.Add(new Sate("El1", "El1", "El1", "Result1"));
        c.Add(new Sate("El2", "El2", "El2", "Result2"));
        c.Add(new Sate("El3", "El3", "El3", "Result3"));
        c.Add(new Sate("El4", "El4", "El4", "Result4"));
        c.Add(new Sate("El5", "El5", "El5", "Result5"));


        string converted = JsonConvert.SerializeObject(c);
        Console.WriteLine(converted);

public class Sate
{
    public Sate(string El, string E2, string E3, string E4)
    {
        e1 = El;
        e2 = E2;
        e3 = E3;
        e4 = E4;
    }

    private string e1 { get; set; }
    private string e2 { get; set; }
    private string e3 { get; set; }
    private string e4 { get; set; }
}

Any idea what i'm missing its been an hour ...

Upvotes: 1

Views: 526

Answers (2)

Brian Rogers
Brian Rogers

Reputation: 129827

Your question doesn't say what isn't working, but I'll venture a guess-- all your class members are marked private, therefore they will not get serialized by default. If you want them to be serialized, either make them public or mark them with [JsonProperty].

Upvotes: 4

loneshark99
loneshark99

Reputation: 734

Your properties are private instead of public.

public string e1 { get; set; }

Upvotes: 3

Related Questions