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
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Convert string with dot notation to JSON Asp.Net

Date- Oct 09,2020

10365

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

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
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, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page