How to serialize JSON that uses keywords for names?

时间:2015-10-30 23:52:38

标签: c# json json.net

I need to make a JSON string like this using JSON.NET: { "description": "the description", "public": true, "files": { "index.html": { "content": "some value" } } } So how can I do that? I tried creating a class for it, but I don't know how to create a field with name "public" because public is C# keyword.

1 个答案:

答案 0 :(得分:2)

The property name could be anything you want, but if you want the value to map to it, you need to add the JsonProperty attribute indicating the name of the property. public class MyObject { public string Description { get; set; } [JsonProperty("public")] public bool IsPublic { get; set; } public Dictionary<string, JObject> Files { get; set; } }