1

i have an object of the following definition

public class @class
{
    public List<ClassHelperNavNode> obj {get; set;}
    public int ID {get;set;}
    public int ParentID {get;set;}
    public string Title {get;set;}

    public @class()
    {
        obj = new List<ClassHelperNavNode>();
    }
}

public class ClassHelperNavNode
{
    public List<ClassHelperNavNode> obj {get; set;}

    public int ID {get;set;}
    public int ParentID {get;set;}
    public string Title {get;set;}
    public string Narrative {get;set;}

    public ClassHelperNavNode()
    {
        obj = new List<ClassHelperNavNode>();
    }
}

Assume there is an object from class @class c1. How do i convert this complex object to anonymous object?

Note: I have updated my post with relevant contructors

here are some pic to show the receving object

picture-1

picture - 2

I need to return an anonymous object using the values in the class (including the nested collections), but rename the property names from

  • ID to id
  • ParentID to parent
  • Title to text
  • Narrative to value
jon dynke
  • 33
  • 6
  • `var data = new { ID = c1.ID, ParentID = c1.ParentID }` creates an anonymous object with 2 properties. You need to give more context as to what your wanting to do. –  Feb 18 '16 at 03:32
  • @StephenMuecke this is relating to that XML to JSON conversion issue i was able to create a class that maps XML nodes now the data is comming to the view properly but i need to convert the object to anonymous – jon dynke Feb 18 '16 at 03:33
  • I know what the issue is from your previous questions, but nobody else reading this does. You need to explain it (i.e. that you need to return Json data to the view but that you need to change the property names from `ParentID` to `parent` and `Narrative` to `value` etc) –  Feb 18 '16 at 03:41
  • @StephenMuecke i have updated with images to show the object structure – jon dynke Feb 18 '16 at 03:41
  • @StephenMuecke oh man that like rewriting the whole two post ! is it valid to put links to those – jon dynke Feb 18 '16 at 03:44
  • Was my assumption in my last comment correct (i.e. you need to change the property names)? If so then that's all you need to put in the question (and the images are irrelevant to your question) –  Feb 18 '16 at 03:47
  • @StephenMuecke yes i need to change the property names from the original names (names in the classes) to names ID ==> id, ParentID==>parent, Title=>text and Narrative=>value – jon dynke Feb 18 '16 at 03:49
  • Then edit your question with that information :) –  Feb 18 '16 at 03:50
  • @StephenMuecke i have updated my post i hope it has relevant information now – jon dynke Feb 18 '16 at 03:52
  • Interesting problem because you have a hierarchical data structure and it could be n levels deep. And it would make more sense if your class definition was `public class @class { public List Narrative { get; set; } public int ID { get; set; } public int ParentID { get; set; } public string Title { get; set; } } (and delete the `ClassHelperNavNode` class) –  Feb 18 '16 at 04:08
  • @StephenMuecke '@class' is like the root node (cus this will be later mapped to a jstree) and all the child nodes of '@class' is represented by ClassHeperNavNode but note that there are three properties common with root and children they are ID, ParentID and Title – jon dynke Feb 18 '16 at 04:14
  • That makes it a bit more difficult and you would need some recursive functions. I think in this case it would be best to use [Json.NET](http://www.newtonsoft.com/json/help/html/Introduction.htm) and use the `[JsonProperty(PropertyName = "...")]` attribute on your properties (refer [this question/answers](http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net) for an example) –  Feb 18 '16 at 04:26
  • @StephenMuecke so i need to create a another class with JSON mapping? – jon dynke Feb 18 '16 at 04:42
  • No, just decorate your existing class properties with the `JosnPropertyAttribute` e.g. `[JsonProperty(PropertyName = "parent")] public int ParentID { get; set; }`. But something seems wrong here. Looking at previous questions from your and your colleagues, it seems you using jstree, which means the class structure you have is not correct. –  Feb 18 '16 at 04:50
  • Your generating a hierarchical structure, so jstree expects `id`, text` and `children` (where `children` is an array). Alternatively you pass a flat structure in which case jstree expects `id`, `parent` and `text` –  Feb 18 '16 at 04:53
  • @StephenMuecke i thought to store the collection in value property not in children. i was searching the net for the last two days on how do deal with jstree control and this XML data and json mayhem .... would you be kind enough to write some skeleton code on how to map the returning collection data with jstree – jon dynke Feb 18 '16 at 05:14
  • Refer [documentation](https://www.jstree.com/docs/json/). If you have a hierarchical structure then it would look like `data: [ { id: `1`, text: 'level 1', children: [ { id: `1`, text: 'level 1.1', children: null , { id: `1`, text: 'level 1.2', children: null } ] } ]`. I suggest you just start testing the control by hard coding in some values so you understand what is the correct structure (both using the standard format and the alternative format) and then build your objects accordingly –  Feb 18 '16 at 05:22
  • @StephenMuecke cheers for that : ) – jon dynke Feb 18 '16 at 05:24

0 Answers0