开发者

Using JSON.Net to write a property name

I am using JSON.net to write some json in C#. I can produce JSON like this

{
    "id": "234",
    "name": "abc"
}

What i would like to do is get is this

 {
    "DATA": {
        "id": "234",
        "name": "abc"
    }
}

Here is the json.net code i'm using开发者_如何学Python

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

can you suggest how to add the 'DATA' section to it?


Make the root object, then write the property name "DATA", then write the object you just wrote:

jsonWriter.WriteStartObject();
    jsonWriter.WritePropertyName("DATA");
    jsonWriter.WriteStartObject();
        jsonWriter.WritePropertyName("id");
        jsonWriter.WriteValue("234");
        jsonWriter.WritePropertyName("name");
        jsonWriter.WriteValue("abc");
    jsonWriter.WriteEndObject();
jsonWriter.WriteEndObject();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜