Create a strongly typed c# object from json object with ID as the name

后端 未结 1 1999
我在风中等你
我在风中等你 2020-11-22 03:10

I am trying to make use of the API for a well known online meeting provider. One of their API calls returns an object that looks like this.

{
    \"5234592\         


        
相关标签:
1条回答
  • 2020-11-22 03:51

    Make your root object be a dictionary:

    var dictionary = JsonConvert.DeserializeObject<Dictionary<string, SessionPerformanceStats>>(theJsonResponse);
    

    Json.NET serializes a dictionary from and to a JSON object, with the keys being converted to the property names. In your case, the ID numbers will be deserialized as the dictionary keys. If you are sure they will always be numbers, you could declare them as such:

    var dictionary = JsonConvert.DeserializeObject<Dictionary<long, SessionPerformanceStats>>(theJsonResponse);
    

    See Serialize a Dictionary and Deserialize a Dictionary

    0 讨论(0)
提交回复
热议问题