Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Achieve json result

Posted on 2016-11-30
2
Medium Priority
?
106 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 35

Accepted Solution

by:
ste5an earned 2000 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

AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

664 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