1

Everything run and Deserialize OK. Except the Roles is show up as System.Collections.Generic.List`1[System.String]

Problem

If I want to attach each field to a grid. Is there a way to show the collection as a string without looping through the Roles property?

JSON

[{
    "Name":"test",
    "Email": "[email protected]",
    "Roles": ["Admin","User","Guest"],
    "Age":"23"
},
{
  "Name":"test1",
  "Email": "[email protected]",
  "Roles": ["Admin", "User" ,"Guest"],
  "Age":"33"
}]

Model

public class example
{
    public string Name { get; set; }
    public string Email { get; set; }
    public IList<string> Roles { get; set; }
    public string Age { get; set; }
}

Deserialization

List<Exampe> list = JsonConvert.DeserializeObject<List<Exampe>>(bundle);
5
  • 1
    You need to clarify what the actual problem is because everything shown above should work as expected. Roles will be deserialized to a list of strings by default based on the type shown for the property. Commented Jul 26, 2018 at 21:09
  • What platform are you using? If Xaml use a value converter. Commented Jul 26, 2018 at 21:18
  • In future note that question like this appears to be an XY problem. Remember that providing a minimal reproducible example should help clarify the actual problem most of the time. Commented Jul 26, 2018 at 21:20
  • I'm using Windows Form, DataGridView. Thank you, Nkosi. Next time, I will be more accurate with question and format. Commented Jul 26, 2018 at 21:24
  • Show how you assign the data to the data grid after deseiralization.. Commented Jul 26, 2018 at 21:42

3 Answers 3

2

As @Nkosi mentioned, this is a XY problem. The problem is not deserialization, but rather how DataGridView handles a "complex" property type.

You can add a new property to display it:

  • Either by directly modifying the class definition:

    public class Example
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string[] Roles { get; set; }
        public string Age { get; set; }
    
        public string RolesText => string.Join(", ", Roles ?? Array.Empty<string>());
    }
    
  • Or by altering the DataSource of the DataGridView:

    dgv.DataSource = list.Select(x => new
    {
        x.Name, x.Email, x.Age,
        Roles = string.Join(", ", x.Roles ?? Array.Empty<string>()),
    }).ToList();
    

--

Bonus: Here is an generator for the 2nd approach:

string GenerateModelFormatter<T>()
{
    return new StringBuilder()
        .AppendLine("x => new")
        .AppendLine("{")
        .AppendLine(string.Join(",\n", typeof(T).GetProperties()
            .Select(x => x.PropertyType != typeof(string[])
                ? $"\t{x.Name} = x.{x.Name}"
                : $"\t{x.Name} = string.Join(\", \", x.{x.Name} ?? Array.Empty<string>())")))
        .AppendLine("}")
        .ToString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I personally prefer the second option. clean and simple.
Thank you all for the help. I was afraid of having to do something like this because I'm dealing with a class that has over 300+ properties and 50% of them will be an array of string[]. Big decision now..
You can use reflection or meta-programming to generate the lambda function:
That is a little bit out of my scope of knowledge but if you can point me in the right direction, i'm willing to learn.
1

You have Name twice in your class - you probably want:

public class example
{
    public string Name { get; set; }
    public string Email { get; set; }
    public IList<string> Roles { get; set; }
    public string Age { get; set; }
}

Comments

1

If I understand correctly you seem to want to get string[] instead of List<string>

Roles can be string[]

public class Example
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string[] Roles { get; set; }
    public string Age { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.