【问题标题】:Deserialize JSON using Entity Framework Core C#使用 Entity Framework Core C# 反序列化 JSON
【发布时间】:2021-02-03 16:21:47
【问题描述】:

基本上,我想获取(直接反序列化为对象)下面 JSON 的“rows”属性下的数据。

我需要创建哪些包装类以便我可以直接使用 JsonConvert.DeserializeObject<'SomeWrapperClass'>(message); 获取这些数据"rows": [ [ 19.545363672276512, "JapanUnifia-Trial", 20180331, "USD" ], [ 173.41979241290323, "RVIIOT-TRIAL", 20180331, "USD" ], [ 20.359416562625452, "VSTSHOL-1595322048000", 20180331, "USD" ] ] 作为一些对象的数组,这些对象可以使用 EF Core 持久化到数据库中。

{
  "id": "providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
  "name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
  "type": "microsoft.costmanagement/Query",
  "properties": {
    "nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query?api-version=2019-10-01&$skiptoken=AQAAAA%3D%3D",
    "columns": [
      {
        "name": "PreTaxCost",
        "type": "Number"
      },
      {
        "name": "ResourceGroup",
        "type": "String"
      },
      {
        "name": "UsageDate",
        "type": "Number"
      },
      {
        "name": "Currency",
        "type": "String"
      }
    ],
    "rows": [
      [
        19.545363672276512,
        "JapanUnifia-Trial",
        20180331,
        "USD"
      ],
      [
        173.41979241290323,
        "RVIIOT-TRIAL",
        20180331,
        "USD"
      ],
      [
        20.359416562625452,
        "VSTSHOL-1595322048000",
        20180331,
        "USD"
      ]
    ]
  }
}

此示例 json 取自 https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage

【问题讨论】:

    标签: c# json entity-framework entity-framework-core


    【解决方案1】:

    以下是获取rows 属性的类:

    public class Properties
    {
        public List<List<object>> rows { get; set; }
    }
    
    public class Root
    {
        public Properties properties { get; set; }
    }
    

    然后DeserializeNewtonsoft.Json

    var values = JsonConvert.DeserializeObject<Root>(message).properties.rows;
    

    要在数据库中使用这些数据,请拥有您的模型类:

    public class dbModel
    {
        public double propertyName { get; set; }
        // Add other properties
    
        //With the name of db model and view model being valid table names
        //the properties being of the same data types as values.rows
    }
    

    还有一个视图模型类:

    public class dbViewModel
    {
        public IEnumerable<dbModel> dbModels { get; set; }
    
        //Use this class to Enumerate through the rows
    }
    

    【讨论】:

    • 在它被反序列化为 List> 行之后,我将不得不遍历每个数组元素 [ 173.41979241290323, "RVIIOT-TRIAL", 20180331, "USD" ] 并创建一个这 4 个值中的对象代表我的 DataModel 和 DB 中的一个表。使用 JsonConvert.DeserializeObject(message).properties.rows; 时是否有直接的方法可以自行反序列化
    • 我现在看到了答案中的缺陷。没有 ViewModel 肯定可以完成。但这是我会考虑并尝试更新我的答案的事情。
    • 无论如何,感谢您的支持和时间。你的想法行得通,我现在正在使用它。
    • 很高兴能提供帮助,在这种情况下我可能不会使用视图模型,但它是可选的,只需遍历values,将每个模型初始化为 db 模型类的实例并添加到分贝
    猜你喜欢
    • 2018-06-13
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2011-05-30
    相关资源
    最近更新 更多