Solved

fomat Json objects

Posted on 2016-11-29
6
18 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:sevensnake77
  • 4
  • 2
6 Comments
 
LVL 19

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:sevensnake77
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:sevensnake77
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 19

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:sevensnake77
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:sevensnake77
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now