-3

I have below class

public class MyClass
{
    public string Name {get; set;}
    public List<string> Products {get; set;}
}

if I use the below code, I am able to get value of '''Name''' property. However not able to get values of Products list.

How to do that?

Type type = user.GetType();
PropertyInfo[] props = type.GetProperties();
string str = "{";
foreach (var prop in props)
{
   str+= (prop.Name+":"+  prop.GetValue(user))+",";
}
return str.Remove(str.Length-1)+"}";
Gaurav123
  • 5,059
  • 6
  • 51
  • 81
  • 4
    Find a json serializer that works for you. There are many on NuGet. What you're doing is the hard way. – Joel Coehoorn May 15 '20 at 23:45
  • 1
    `user` is an instance of `MyClass`? – Mohammed Sajid May 15 '20 at 23:54
  • I know people who do things like write their own serializers. They make everyone sad. – Aluan Haddad May 16 '20 at 00:14
  • 1
    how about the result if you have a nested class property in your class `MyClass`?how the data will looks like for the list of string?`prop.GetValue(user)` will gives you just the type, like `"System.Collections.Generic.List\`1[System.String]"` – Mohammed Sajid May 16 '20 at 00:14
  • @Sajid Yes I am getting the same you mentioned, instead of "System.Collections.Generic.List`1[System.String]" I need proper values say "Product1" and "Product2" , How to achieve that? – Gaurav123 May 16 '20 at 00:19
  • @Gaurav123 `if (property.GetValue(user, null) is IList list)` and take each element for ``list`` inside if. – Mohammed Sajid May 16 '20 at 00:21
  • Please rephrase your question and explain what you are trying to do explicitly. – Aluan Haddad May 16 '20 at 00:23

1 Answers1

-2

Add a ToJson() method to the type:

public class MyClass
{
    public string Name {get; set;}
    public List<string> Products {get; set;}

    public string ToJson()
    {
        var result = new StringBuilder("{");
        result.Append("\"Name\":\"")
              .Append(Name)
              .Append("\",{\"Products\":[{\"Product\":\"");

        var delimiter = "";
        foreach(var product in Products)
        {
           result.Append(delimter).Append(product);
           delimiter = "\"},{\"Product\":\"";
        }
        result.Append("\"}]}");
        return result.ToString();
    }
}

Then the code becomes:

string str = user.ToJson();

If you need to do this for a number of different types, create an interface they can implement:

interface ICanHazJson
{
    string ToJson();
}

And then make each of the types you care about implement the interface:

public class MyClass : ICanHazJson
{
    public string Name {get; set;}
    public List<string> Products {get; set;}

    public string ToJson()
    {
        var result = new StringBuilder("{");
        result.Append("\"Name\":\"")
              .Append(Name)
              .Append("\",{\"Products\":[{\"Product\":\"");

        var delimiter = "";
        foreach(var product in Products)
        {
           result.Append(delimter).Append(product);
           delimiter = "\"},{\"Product\":\"";
        }
        result.Append("\"}]}");
        return result.ToString();
    }
}

And if there are many types where this matters, that's all the more reason to find a battle-tested json serializer on NuGet that will do what you need and not blow up on some unexpected edge case.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks for response, actually I need generic solution. I want to pass just a class name and that class can have n number of properties including string, int and List properties. Once I will pass class name I need to format properties name and their values but not to JSON – Gaurav123 May 16 '20 at 00:03
  • The format demonstrated in the question **IS** json. – Joel Coehoorn May 16 '20 at 21:59