【问题标题】:How to add JObject to Entity Framework as an object如何将 JObject 作为对象添加到实体框架
【发布时间】:2020-04-22 21:57:25
【问题描述】:

我有一个代表反序列化 json jobjects 的变量列表,我想将它们添加到实体数据库中。我该怎么做?

目前尝试的解决方案:

var obj = JsonConvert.DeserializeObject<dynamic>(strtest);

foreach (var item in obj.results)
{
    db.ReedAPIModels.Add(item);
    db.SaveChanges();
}

模型类代表数据库:

public class APIModel
{
        [Key]
        public int jobId { get; set; }
        public int employerId { get; set; }
        public string employerName { get; set; }
        public string employerProfileId { get; set; }
        public string employerProfileName { get; set; }
        public string jobTitle { get; set; }
        public string locationName { get; set; }
        public int minimumSalary { get; set; }
        public int maximumSalary { get; set; }
        public string currency { get; set; }
        public string expirationDate { get; set; }
        public string date { get; set; }
        public string jobDescription { get; set; }
        public int applications { get; set; }
        public string jobUrl { get; set; }
}

API 结果

{
  "results": [
    {
      "jobId": 39650785,
      "employerId": 375315,
      "employerName": "Spectrum IT Recruitment (South)",
      "employerProfileId": null,
      "employerProfileName": null,
      "jobTitle": "Senior Front End Developer",
      "locationName": "Reading",
      "minimumSalary": 60000,
      "maximumSalary": 70000,
      "currency": "GBP",
      "expirationDate": "17/01/2020",
      "date": "03/01/2020",
      "jobDescription": " Senior Front End Developer - Reading, Berkshire  Senior Front End Developer required by a growing company based in Reading, Berkshire. The company are looking to recruit a Senior Front End Developer to join their expanding development team to work on the full design and development lifecycle of new products. The successful Senior Front End Developer will have lots of opportunities for development of both techni... ",
      "applications": 0,
      "jobUrl": "https://www.reed.co.uk/jobs/senior-front-end-developer/39650785"
    }
  ],
  "ambiguousLocations": [],
  "totalResults": 9007
}

另外,我尝试过使用ToObject方法

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework asp.net-web-api


    【解决方案1】:

    您应该添加APIModel 而不是ReedAPIModels

    你可以试试这个方法

    var apiResult = JsonConvert.DeserializeObject<dynamic>(strtest);
    
    foreach (var item in apiResult.results)
    {
        db.APIModel.Add(item);
        db.SaveChanges();
    }
    

    【讨论】:

    • 需要RooObject模型
    • foreach循环中也需要关键字result
    • 你为什么不给我你的样本strtest值,我认为它应该与APIModel具有相同的结构
    • 是的,您应该在开头提供API Results
    • 我刚刚更新了我的答案,请看看并尝试一下,然后反馈给我@ghassa
    【解决方案2】:

    首先你需要一个可以由这个网站创建的 RootObject 模型: http://json2csharp.com/

        RootObject datalist = JsonConvert.DeserializeObject<RootObject>(strtest);
    
        foreach (var item in datalist.results)
        {
            db.APIModels.Add(item);
            db.SaveChanges();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 2021-10-16
      相关资源
      最近更新 更多