Solved

Achieve json result

Posted on 2016-11-30
2
96 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: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
2 Comments
 
LVL 34

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:Seven price
ID: 41908724
tks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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