Treatment JSON back serialization with an invalid C#identifier
Use the dictionary
The following is an example of handling this situation with dictionary:
For the JSON string in the problem:
<code class="language-csharp">public class Item { public string fajr { get; set; } public string sunrise { get; set; } public string zuhr { get; set; } public string asr { get; set; } public string maghrib { get; set; } public string isha { get; set; } } // 将JSON字符串反序列化为字典 var dict = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);</code>
You can use the string key ("1" and "2") to access the data in the dictionary, for example:
<code class="language-json">{ "1": { "fajr": "04:15", "sunrise": "05:42", "zuhr": "12:30", "asr": "15:53", "maghrib": "19:18", "isha": "20:40" }, "2": { "fajr": "04:15", "sunrise": "05:42", "zuhr": "12:30", "asr": "15:53", "maghrib": "19:18", "isha": "20:41" } }</code>
The above is the detailed content of How to Deserialize JSON with Illegal C# Identifiers?. For more information, please follow other related articles on the PHP Chinese website!