Solved

fomat Json objects

Posted on 2016-11-29
6
37 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
[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: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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

734 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