Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4624242
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:06:08+00:00 2026-05-22T03:06:08+00:00

I’m trying to properly write code to build a data structure to serialize into

  • 0

I’m trying to properly write code to build a data structure to serialize into json.

I’m using json.net.

I don’t want to create a bunch of classes to hold this data, as I thought there should be some classes that will already do this in json.net

I’ve already got all the data I need in a series of nested loops, and now I just want to add them to an object hierarchy before I run JsonConvert.SerializeObject on it.

I’ve already tried code like this, but it doesn’t seem to work

JArray container = new JArray();

        container.Add(new JObject(new JProperty("name", "Client1"), new JProperty("projects", new JArray())));

        container[0].AddAfterSelf(new JObject(new JProperty("projects", new JArray())));            
        container[1].AddAfterSelf(new JObject(new JProperty("projects", "Project2")));
        container[1].AddAfterSelf(new JObject(new JProperty("projects", "Project3")));
        container.Add(new JProperty("name", "Client2"));            

        var test = JsonConvert.SerializeObject(container);

The problem is that when I use [i]. or ElementAt(i) to access somewhere in the structure, either .Add() is missing or .ElementAt isn’t there.
How do I step through the data structure to make this nicely output the below, or do I have to create my own container class for all of this?

This is the data format I am trying to make.

[
    {
    "name": "student1",
    "projects": 
    [
        {
        "name": "Project1",
        "tasks": 
                [
                    {
                    "name": "task1",
                    "id": 2
                    }
                ],
        "id": 6
        }
    ]
},
    {
    "name": "Student2",
    "projects": [
                {
                "name": "Project1",
                "tasks": [
                         {
                         "name": "Task2",
                         "id": 1
                         },
                         {
                         "name": "Task3",
                         "id": 3
                         },
                         {
                         "name": "Task4",
                         "id": 4
                         }
                         ],
                "id": 2

etc…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:06:09+00:00Added an answer on May 22, 2026 at 3:06 am

    I think what you’re asking is how to serialize complex business objects in json, but only expose certain properties.

    In other words you already have a list of students, but you only want to send very specific data via json. If I’m wrong this answer won’t suit your needs.

    So assuming you have a list of students, with a projects property that has an inner property of tasks, this is how I do it without having to create loads of new classes, I use anonymous objects.

    Once I’ve created my list of anonymous objects, I simply turn them into a json string.

    As pointed out in the comments you don’t need to use json.net, this functionality is available in the framework, add a reference to System.Web.Extensions.dll then

    using System.Web.Script.Serialization;

    var jsonStudents = new List<object>();
    
    foreach (var student in students)
    {
        jsonStudents.Add(new
        {
            student.Id,         //anonymous properties automatically pick up the name of the property you pass them, this will be called Id
            FullName = student.FirstName + " " + student.LastName, //if you want to name a property yourself use this notation
            Projects = student.Projects.Select(p => new //this will be an enumerable of nested anonymous objects, we're partially selecting project properties
            {
                p.Id,
                p.Name,
                Tasks = p.Tasks.Select(t => new //nesting another level
                {
                    t.Id,
                    t.Name
                })
            })
        });
    }
    
    var serializer = new JavaScriptSerializer();
    
    var jsonString = serializer.Serialize(jsonStudents);
    

    If you really want to use loops you can do this to allow you to do more complicated things in the creating of the projects and tasks:

    var jsonStudents = new List<object>();
    
    foreach (var student in students)
    {
        var tempStudent = new
        {
            student.Id,         //anonymous properties automatically pick up the name of the property you pass them, this will be called Id
            FullName = student.FirstName + " " + student.LastName, //if you want to name a property yourself use this notation
            Projects = new List<object>()
        };
    
        foreach (var project in student.Projects)
        {
            var tempProject = new {
                project.Id,
                project.Name,
                Tasks = new List<object>()
            };
    
            foreach (var task in project.Tasks)
            {
                tempProject.Tasks.Add(new {
                    task.Id,
                    task.Name
                });
            }
    
            tempStudent.Projects.Add(tempProject);
        }
    
        jsonStudents.Add(tempStudent);
    }
    
    var serializer = new JavaScriptSerializer();
    
    var jsonString = serializer.Serialize(jsonStudents);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.