Convert string with dot notation to JSON Asp.Net | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Convert string with dot notation to JSON Asp.Net

Date- Oct 09,2020

7724

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);

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1210
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts