Code2night
  • Home
  • Blogs
  • Tutorial
  • Post Blog
  • Tools
    • Json Beautifier
    • Html Beautifier
  • Members
    • Register
    • Login
  1. Home
  2. Blogpost
09 Oct
2020

Convert string with dot notation to JSON Asp.Net

by Shubham Batra

4163

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

  • |
  • JSON , Convert string with dot notation to JSON

Comments

Follow Us On Social Media - Like Us On Facebook

Best Sellers

product 1

Hand Hug Bracelet For Women Men Cuff Bangle Adjustable Lover Couple Bracelets

Can be given as a gift to your family, relatives, or friends

Buy $15.99
product 1

Teddy bear hug bear plush toy bear cub

Can be given as a gift to your family, relatives, or friends


Buy $49.99

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
Thank you for Downloading....!

Subscribe for more tutorials

Support our team

Continue with Downloading

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 © 2023 by Code2night. All Rights Reserved

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