Solved

Achieve json result

Posted on 2016-11-30
2
64 Views
Last Modified: 2016-12-01
 public class Child
    {
        public int ids { get; set; }
        public string names { get; set; }
      
    }

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

Open in new window


From above  how we can achieve the result for my Json to render

 [
        {
            id: 1,
            names: 'root1',
            children: [
                { id: 2, name: 'child1' },
                { id: 3, name: 'child2' }
            ]
        },
       
    ];

Open in new window

my Local variable creating an instance.
What is going on, the application connection to a folder. depending on how many files it loops through.
The problem is I cannot return the above structure in children correctly. kind of stuck and know I am missing something small.
Can anyone make example to achieve these result, not link to websites.

var attachmentsList = new List<RootObject>();
FileInfo[] files = dinfo.GetFiles();

                foreach (FileInfo file in files)
                {

                    attachmentsList.Add(new RootObject
                    {
                        id = i + 1,
                        name = "Folder" + i + 1,
                        children = new List<Child>(
                            id = i + 1,
                            file.Name
                        )

                    });
 i++;
}

Open in new window

0
Comment
Question by:sevensnake77
2 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 41908324
Use the built-in serializer.

E.g.
namespace ConsoleCS
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Json;

    class Program
    {
        static void Main(string[] args)
        {
            Root root = new Root() { id = 1, name = "root", children = new List<Child>() };
            root.children.Add(new Child() { id = 10, name = "child1" });
            root.children.Add(new Child() { id = 20, name = "child2" });
            using (MemoryStream stream = new MemoryStream())
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Root));
                ser.WriteObject(stream, root);
                using (StreamReader reader = new StreamReader(stream))
                {
                    stream.Position = 0;
                    Console.WriteLine(reader.ReadToEnd());
                }
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }

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

    }

    [DataContract]
    public class Root
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public List<Child> children { get; set; }
    }
}

Open in new window


See also JSON Serialization.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 41908724
tks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

19 Experts available now in Live!

Get 1:1 Help Now