2016-08-04 33 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