0

I can't find any solution for my case.I recieve JSON and then I parsed it to my class Instance.In the instance I have property object Model that must be maped from JSON object model.All is good but in this model I don't know how many and which property I can recieve. I want to iterate all props in model and check theirs type.I need this because I want to generate EditForm from model properties.I was tried with

foreach(PropertyInfo prop in Model.GetType().GetProperties())

but it looks the model is type of NewtonSoft.Json.Linq.JObject.

class Instance
{
    public object Model { get; set; }
    public List<Label> Labels { get; set; }
    public List<Instance> Children { get; set; }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Did you [check the docs](https://www.newtonsoft.com/json/help/html/JObjectProperties.htm)? You can use `JObject.Properties` although a better option may be to deserialize the JSON string into a concrete object and make `Instance` a generic class. In any case, change `object` to `JObject`. – Panagiotis Kanavos May 10 '21 at 13:02
  • 1
    As it so happens `JObject` implements `ICustomTypeDescriptor` so you can use `TypeDescriptor.GetProperties(obj)` to get the properties of an `Instance` or `JObject` in a uniform way, see https://dotnetfiddle.net/TssIx7. But if you want to get the properties of a POCO such as `Instance` or a `DynamicObject` such as `ExpandoObject` or an entirely custom dynamic object such as `JObject` in a uniform way, see [How do I reflect over the members of dynamic object?](https://stackoverflow.com/q/2634858/3744182). – dbc May 10 '21 at 14:27
  • Or you could just test to see whether the object is a `JObject` and use `JObject.Properties` as suggested by Panagiotis Kanavos. – dbc May 10 '21 at 14:29
  • I am not sure how It must to look this in JSON.Because I cant get the property value with name no problem."id": null, "caption": "Contactgegevens", "concept": "duo-abstr_ContactDetailsTitle", "model": [ { "Geslachtsaanduiding": "Man" }, { "Voornaam": "Piet" }, { "Achternaam": "van" }, { "Dijk": "Bestuursnummer" }, { "1111": "Straatnaam" }, { "NL": "Straatweg" }, –  May 10 '21 at 20:08

1 Answers1

0

I found a solution for this problem. All is in my JSON file structure. I maked model him like this:

model:[ {"key":"someKey","value":"someValue"}]
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291