2016-08-04 53 views
0

我想遍历这我如返回,看起来像这样我的JSON数据:保存JSON属性值来建模

"id": 9493731, 
"due_at": null, 
"unlock_at": null, 
"lock_at": null, 
"points_possible": 1, 
"grading_type": "pass_fail", 
"assignment_group_id": 2645710, 
"grading_standard_id": null, 
"created_at": "2016-04-13T18:30:41Z", 
"updated_at": "2016-08-04T05:30:03Z", 
"peer_reviews": false, 
"automatic_peer_reviews": false, 
"position": 6, 
"grade_group_students_individually": null, 
"anonymous_peer_reviews": null, 
"group_category_id": null 

我想检索属性名称,并将其保存到我的模型它看起来像这样:

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

namespace CanvasAPI.Model 
{ 
    public class Assignment 
    { 
     [Key] 
     public int? AssignmentID { get; set; } 
     public int? id { get; set; } 
     public string name { get; set; } 
     public string description { get; set; } 
     public string created_at { get; set; } 
     public string updated_at { get; set; } 
     public string due_at { get; set; } 
     public string lock_at { get; set; } 
     public string unlock_at { get; set; } 
     public bool has_overrides { get; set; } 
     //public All_Dates[] all_dates { get; set; } 
     public int? course_id { get; set; } 
     public string html_url { get; set; } 
     public string submissions_download_url { get; set; } 
     public int? assignment_group_id { get; set; } 
     public string[] allowed_extensions { get; set; } 
     public bool turnitin_enabled { get; set; } 
     //public Turnitin_Settings turnitin_settings { get; set; } 
     public string originality_report_visibility { get; set; } 
     public bool s_paper_check { get; set; } 
     public bool internet_check { get; set; } 
     public bool journal_check { get; set; } 
     public bool exclude_biblio { get; set; } 
     public bool exclude_quoted { get; set; } 
     public string exclude_small_matches_type { get; set; } 
     public int? exclude_small_matches_value { get; set; } 
     public bool grade_group_students_individually { get; set; } 
     //public External_Tool_Tag_Attributes external_tool_tag_attributes { get; set; } 
     public bool peer_reviews { get; set; } 
     public bool automatic_peer_reviews { get; set; } 
     public int? peer_review_count { get; set; } 
     public string peer_reviews_assign_at { get; set; } 
     public bool anonymous_peer_reviews { get; set; } 
     public bool moderated_grading { get; set; } 
     public int? group_category_id { get; set; } 
     public int? needs_grading_count { get; set; } 
     //public Needs_Grading_Count_By_Section[] needs_grading_count_by_section { get; set; } 
     public int? position { get; set; } 
     public bool post_to_sis { get; set; } 
     public string integration_id { get; set; } 
     public string integration_data { get; set; } 
     public bool muted { get; set; } 
     public bool has_submitted_submissions { get; set; } 
     public float? points_possible { get; set; } 
     public string submission_types { get; set; } 
     public string grading_type { get; set; } 
     public int? grading_standard_id { get; set; } 
     public bool published { get; set; } 
     public bool unpublishable { get; set; } 
     public bool only_visible_to_overrides { get; set; } 
     public bool locked_for_user { get; set; } 
     //public Lock_Info lock_info { get; set; } 
     public string lock_explanation { get; set; } 
     public int? quiz_id { get; set; } 
     public bool anonymous_submissions { get; set; } 
     public string discussion_topic { get; set; } 
     public bool freeze_on_copy { get; set; } 
     public bool frozen { get; set; } 
     public string[] frozen_attributes { get; set; } 
     public string submission { get; set; } 
     public bool use_rubric_for_grading { get; set; } 
     public string rubric_settings { get; set; } 
     //public Rubric[] rubric { get; set; } 
     public int?[] assignment_visibility { get; set; } 
     //public Override[] overrides { get; set; } 

} 

我能够通过JSON数据迭代和检索值,但我有麻烦试图将数据保存到模型。

这是我到目前为止有:

      Assignment assignment = new Assignment(); 

          foreach (string myVal in name_list) 
          { 
           if (item[myVal].ToString() != string.Empty) 
           { 
           var a = "assignment" + "." + myVal; 
      **problem here -->** a = item[myVal].ToString(); 
           } 
          } 

          db.Assignments.Add(assignment);db.SaveChanges();` 

我怎么能动态分配的属性名称和值,以我的模型,这样我可以保存更改?

+0

不要那样做 - 使用newtonsoft –

回答

1

您可以使用newtonsoft.json来实现此目的。 继承人的连结,例如: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

您的代码应该是这样的:

Assignment assignment = JsonConvert.DeserializeObject<Assignment>(jsonString); 

的JSON是desearized为对象,通过名称映射属性。

希望它有帮助

+0

好的。我进一步了解这一点,但现在我得到一个错误'错误阅读字符串。意外的令牌“,它指向我的JSON数据的这一部分,如下所示:'”integration_data“:{}'正如您所看到的,该属性中没有数据。在反序列化方法中处理这个问题的最好方法是什么? – George

+0

事实证明,integration_data实际上是一个对象。我刚刚在我的模型中创建了另一个班级,并已经过去了这个问题。 – George