How to build object hierarchy for serialization with json.net?

后端 未结 3 776
醉梦人生
醉梦人生 2020-12-24 02:30

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

3条回答
  •  暖寄归人
    2020-12-24 02:58

    this is the code which generates the exact output from your question (requires a using Newtonsoft.Json.Linq;):

    var json = new JArray(
                    new JObject(
                        new JProperty("name", "student1"),
                        new JProperty("projects", 
                            new JArray(
                                new JObject(
                                    new JProperty("name", "Project1"),
                                    new JProperty("tasks", 
                                        new JArray(
                                            new JObject(
                                                new JProperty("name", "task1"),
                                                new JProperty("id", 2)
                                                )
                                            )
                                        ),
                                    new JProperty("id", 6)
                                )
                            )
                        )
                    ),
                    new JObject(
                        new JProperty("name", "student2"),
                        new JProperty("projects", 
                            new JArray(
                                new JObject(
                                    new JProperty("name", "Project1"),
                                    new JProperty("tasks", 
                                        new JArray(
                                            new JObject(
                                                new JProperty("name", "task2"),
                                                new JProperty("id", 1)
                                                ),
                                            new JObject(
                                                new JProperty("name", "task3"),
                                                new JProperty("id", 3)
                                                ),
                                            new JObject(
                                                new JProperty("name", "task4"),
                                                new JProperty("id", 4)
                                                )
                                            )
                                        ),
                                    new JProperty("id", 2)
                                )
                            )
                        )
                    )
                );
    var jsonString = json.ToString();
    

    I believe using the Json.Net Linq syntax has the great advantage that the resulting C# code can be formatted so that it almost has the same structure as the JSON you're trying to generate.

    UPDATE

    If you want to manipulate the Json object once it has been built, look at this example which builds the outer array with only one student, and then appends another one:

    // create an isolated Student instance:
    var student2 = new JObject(
                        new JProperty("name", "student2"),
                        new JProperty("projects", 
                            new JArray(
                                new JObject(
                                    new JProperty("name", "Project1"),
                                    new JProperty("tasks", 
                                        new JArray(
                                            new JObject(
                                                new JProperty("name", "task2"),
                                                new JProperty("id", 1)
                                                ),
                                            new JObject(
                                                new JProperty("name", "task3"),
                                                new JProperty("id", 3)
                                                ),
                                            new JObject(
                                                new JProperty("name", "task4"),
                                                new JProperty("id", 4)
                                                )
                                            )
                                        ),
                                    new JProperty("id", 2)
                                )
                            )
                        )
                    );
    
    var json = new JArray(
                    new JObject(
                        new JProperty("name", "student1"),
                        new JProperty("projects", 
                            new JArray(
                                new JObject(
                                    new JProperty("name", "Project1"),
                                    new JProperty("tasks", 
                                        new JArray(
                                            new JObject(
                                                new JProperty("name", "task1"),
                                                new JProperty("id", 2)
                                                )
                                            )
                                        ),
                                    new JProperty("id", 6)
                                )
                            )
                        )
                    )
                );
    
    // now, add the student2 instance to the array:
    json                             // which is an JArray
        .Last                        // gets the last Array item, i.e. "student1"
        .AddAfterSelf(student2);     // adds this which hence becomes the new last one
    

    The idea is that you can apply the very same principle to any other portion of the structure in the same way.

    HTH...

提交回复
热议问题