Solved

Achieve json result

Posted on 2016-11-30
2
24 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 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
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
Comment Utility
tks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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