Solved

Achieve json result

Posted on 2016-11-30
2
87 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
[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:sevensnake77
ID: 41908724
tks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need help writing Unit Tests for my ASP.Net MVC application 5 40
Excel Web Add-in Where is Visual Basic used 9 84
Load XML element 3 43
Datagridview column resizing 8 28
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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