Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Convert string with dot notation to JSON Asp.Net

Date- Oct 09,2020

10327

JSON

Issue-

You might have faced issue converting string with dot notation to JSON Asp.Net. Have a look at this image

So as in the image you can see we have dot separated hierarchy which we have to convert in json. So we will have a look at how to do that in c#.

Step-1

So we will start by creating  a class for our hierarchical data

public class Treepath
    {
        public string Key { get; set; }
        public Treepath children { get; set; }

    }
Now you can use this piece of code for converting your dot notation to JSON

List<Treepath> trees = new List<Treepath>();
            foreach (var path in treepath)
            {
                var item = new Treepath();
                var sublevel = path.Split(".");
                for (int i = 0; i < 1; i++)
                {
                    var lastitem = new Treepath();
                    for (int j = sublevel.Count()-1; j > 0; j--)
                    {
                        if (sublevel[j] == sublevel.LastOrDefault())
                        {
                            lastitem.children = new Treepath { Key = sublevel[j], children = null };
                        }
                        else
                        {
                            lastitem.children = new Treepath { Key = sublevel[j], children =  lastitem.children  };
                        }
                    }
                    lastitem.Key = sublevel[i];
                    item = lastitem;
                }
                trees.Add(item);
            }
            var json = JsonConvert.SerializeObject(trees);

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to read json in Sql Server
Aug 10, 2022

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers