使用JSON.Net编写属性名称

she*_*ill 9 c# json json.net

我正在使用JSON.net在C#中编写一些json.我可以像这样生成JSON

{
    "id": "234",
    "name": "abc"
}
Run Code Online (Sandbox Code Playgroud)

我想做的就是得到这个

 {
    "DATA": {
        "id": "234",
        "name": "abc"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的json.net代码

    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    JsonWriter jsonWriter = new JsonTextWriter(sw);
    jsonWriter.Formatting = Formatting.Indented;



        jsonWriter.WriteStartObject();                 
            jsonWriter.WritePropertyName("id");
            jsonWriter.WriteValue("234");
            jsonWriter.WritePropertyName("name");
            jsonWriter.WriteValue("abc");
        jsonWriter.WriteEndObject();
Run Code Online (Sandbox Code Playgroud)

你能建议如何添加'DATA'部分吗?

Bol*_*ock 14

创建根对象,然后编写属性名称"DATA",然后编写刚才写的对象:

jsonWriter.WriteStartObject();
    jsonWriter.WritePropertyName("DATA");
    jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("id");
        jsonWriter.WriteValue("234");
        jsonWriter.WritePropertyName("name");
        jsonWriter.WriteValue("abc");
    jsonWriter.WriteEndObject();
jsonWriter.WriteEndObject();
Run Code Online (Sandbox Code Playgroud)