c# - Deserialize JSON object that has the same object as member in it -


i having trouble de-serializing json has structure following.

public class entity {     public int id;     public string entityname;     private list<entity> _items;      //sub entities     public ireadonlylist<entity> items     {         get{                return _items;            }     } } 

while in debug mode, able see items > 0 via json visualizer(as in below image) when try de-serialize items count = 0

enter image description here

i tried using following statements, still no luck.

var json = await response.getcontentasync<string>(); 

try 1

var deserializedlist = jsonconvert.deserializeobject<ienumerabld<entity>>json); 

try 2

var settings = new jsonserializersettings{maxdepth = 5}; var deserializedlist = jsonconvert.deserializeobject<ireadonlylist<entity>>(json, settings); 

any regarding appreciated.

the first option make ilist<entity> if want keep ireadonlylist<entity> can supplying setter -

    public ireadonlylist<entity> items     {                 {             return _items;         }         set         {             _items = _items ?? new list<entity>();             if (value == null)                 return;             _items.addrange(value);         }     } 

here example -

enter image description here


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -