反序列化动态 JSON 对象

scu*_*lly 3 .net c# json deserialization asp.net-core

我正在使用第三方 API,收到的 Json 响应如下。通常,使用类似的东西

Newtonsoft.Json.JsonConvert.DeserializeObject<MyModel>(response);
Run Code Online (Sandbox Code Playgroud)

我可以轻松创建 .Net 模型。然而,我正在努力思考如何制定以下内容。我尝试过 KeyValuePairs、字符串、修改实际的 Json 但似乎无法获得任何乐趣。

我缺少什么?

{
  "1": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 71.68
    }
  ],
  "2": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 62.75
    }
  ],
  "3": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 77.28
    }
  ],
  "4": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 82.88
    }
  ],
  "5": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 67.84
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在,让我困惑的是数字(1,2,3,4,5)是标识符,因此不会保持不变,并且每次收到响应时都可能会发生变化。

Joh*_*oes 5

我认为 Newtonsoft 可以为您做到这一点。

string json = @"{
  'Email': 'james@example.com',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";

var jsonReturn = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>( json );
Console.WriteLine( jsonReturn.Email );
Run Code Online (Sandbox Code Playgroud)

基于此链接: https ://www.newtonsoft.com/json/help/html/DeserializeObject.htm

这是最基本的解释。使用 foreach 迭代 de 节点