Solved

fomat Json objects

Posted on 2016-11-29
6
39 Views
Last Modified: 2016-11-30
in c# need to create this structure.  Adding the children object I seem to be having trouble with children property  to render below.
Current code renders like
 [
        {
            id: 1,
            name: 'root1',
            children: [
               "id:" 2, name: 'child1' ,
               "id:" 3, name: 'child2' 
            ]
        }
    ];

Open in new window



The way it should render is like this below.
 [
        {
            id: 1,
            name: 'root1',
            children: [
                { id: 2, name: 'child1' },
                { id: 3, name: 'child2' }
            ]
        }
    ];

Open in new window


properties.

        public int id { get; set; }
        public string name { get; set; }
        public IList<string> children { get; set; }

Open in new window



class

 var attachmentsList = new List<ModelDto>();
 FileInfo[] files = dinfo.GetFiles();
                
                foreach (FileInfo file in files)
                {
                    attachmentsList.Add(new ModelDto
                    {
                      
                        id = i + 1,
                        name = "Folder " + i + 1,
                        children = new List<string>
                         {
                            id = i + 1,
                            name = file.name,
                   
                        }
                    });
     
                    i++;

                }

Open in new window

0
Comment
Question by:Seven price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 41906653
This is the site: http://json2csharp.com/#


public class Child
{
    public int id { get; set; }
    public string name { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Child> children { get; set; }
}

Open in new window

0
 
LVL 9

Author Comment

by:Seven price
ID: 41906698
Thanks Daniel. Question Creating the properties not the problem the code in the correct format to display
0
 
LVL 9

Author Comment

by:Seven price
ID: 41906729
 var attachmentsList = new List<RootObject>();
 attachmentsList.Add(new RootObject
                    {
                        id = i + 1,
                        name = "Folder" + i + 1,
                        children = new List<Child>()

Open in new window

Ok this is where I get stuck at. Cannot add the name and the id inside the children also.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41907559
This?

            var attachmentList = new List<RootObject>();
            attachmentList.Add(
                new RootObject()
                {
                    id = i + 1,
                    name = "Folder" + (i + 1).ToString(),
                    children = new List<Child>()
                });

Open in new window

0
 
LVL 9

Author Comment

by:Seven price
ID: 41907844
yes but   children = new List<Child>() cannot add the other properties. tryied the other ways in json.net but since I have to return a list the other ways will not let me.
0
 
LVL 9

Author Closing Comment

by:Seven price
ID: 41907897
thanks
i got it.
    children = new List<Child>()
                    };
                    rootObject.children.Add(new Child { id = i + 1, name = file.Name });
                    i++;
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question