1

i want this:

"collection": {
                    "id": "89999999",
                    "amount": "104",
                    "dateCreated": "2022-01-01"
                }

to become this

"collection": [
                    "id: 89999999",
                    "amount: 104",
                    "dateCreated: 2022-01-01"
              ]

ive tried

 ((IEnumerable)obj).Cast<object>().Select(x => x.ToString()).ToArray()

but it crashes with the message "Unable to cast type "myobject" to IEnumerable"

any idea on how to proceed? or how to somehow convert a object/model to an array, specifically an string array, like the example above?

SOLVED

added this method to my class

        public string[] ConvertToStringArray()
        {

            List<string> lst = new List<string>();

            foreach (var prop in this.GetType().GetProperties())
            {
               if(prop.GetValue(this) != null)
                {
                    lst.Add(prop.Name + ": " + prop.GetValue(this));
                }
            }

            return lst.ToArray();

        }

and then, just

obj.ConvertToStringArray()
philipcode
  • 35
  • 5
  • 2
    If you know the shape of the object then you can do this manually. But if you don't know the shape of the object then this will involve using reflection to get its property names/values. – David Feb 21 '22 at 14:34
  • There are multiple approaches - write custom serializer, use reflection, manually create an array. – Guru Stron Feb 21 '22 at 14:34
  • 2
    Show us your object definition, please, you can use reflection but perhaps you're having an XY problem – J.Salas Feb 21 '22 at 14:34
  • I assume you've already deserialized since you have a `myobject` class - can you post your classes/code so far? You've only shown one line so we are short on context. – Charleh Feb 21 '22 at 14:40
  • Maybe you are looking for [How can I deserialize JSON to a simple Dictionary in ASP.NET?](https://stackoverflow.com/q/1207731/880990). – Olivier Jacot-Descombes Feb 21 '22 at 15:03
  • You can look at this question. It seems to be the same issue: https://stackoverflow.com/questions/9586585/convert-json-to-a-c-sharp-array – Gabriel Tonello Feb 21 '22 at 15:08
  • thanks for the push in the right direction guys, solved it with reflections. Updated original post. – philipcode Feb 21 '22 at 15:08
  • I'm glad you were able to solve it. However, if you share your class definition, we may be able to provide a better solution. – Dan Csharpster Feb 21 '22 at 20:03
  • @DanCsharpster im not quite sure what you mean? "obj" is just an object mapped to an model with predefined properties, thats it. Then i use "JsonConvert.SerializeObject" on the whole object to make it json. – philipcode Feb 22 '22 at 07:42
  • So if this is just a POCO with a bunch of string properties, then this should work. You may want to ask a few questions, though. For example, if this class could include nonstring properties, you may want to add a string check like: if (propertyInfo.PropertyType == typeof(string)). Also, if this is just a POCO with a big pile of string properties and you want a generic way to output them, is it possible to just replace your class with a Dictionary? Then it would be even simpler to output and you would gain some extra capabilities. – Dan Csharpster Feb 23 '22 at 14:10

0 Answers0