2016-08-04 3 views
0

ich über meine JSON-Daten iterieren möchte Modell, das ich kehre zurück und sieht aus wie das zum Beispiel:Speichern JSON Eigenschaftswerte

"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 

Ich möchte die Eigenschaftsnamen abzurufen und sie zu meinem Modell speichern die wie folgt aussieht:

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; } 

} 

ich bin in der Lage durch die JSON-Daten zu durchlaufen und die Werte abrufen, aber ich habe Probleme beim Versuch, die Daten an das Modell zu speichern.

Hier ist, was ich bisher:

      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();` 

Wie kann ich die Eigenschaftsnamen und Werte dynamisch zu meinem Modell zuweisen, so dass ich die Änderungen speichern kann?

+0

Sie das nicht tun - verwenden newtonsoft –

Antwort

1

Sie können newtonsoft.json verwenden, um dies zu erreichen. Heres ein Link zu einem Beispiel: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Der Code sollte wie folgt aussehen:

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

Die json in das Objekt desearized, Abbilden der Eigenschaften nach dem Namen.

Hoffe, es hilft

+0

Ok. Ich komme weiter, aber jetzt bekomme ich einen Fehler 'Error Reading String. Unerwartetes Token "und es zeigt auf diesen Abschnitt meiner JSON-Daten, der wie folgt aussieht:' "integration_data": {} 'Wie Sie sehen können, gibt es in dieser Eigenschaft keine Daten. Was ist der beste Weg, dies innerhalb der Deserialize-Methode zu handhaben? – George

+0

Es stellt sich heraus, dass integration_data tatsächlich ein Objekt war. Ich habe gerade eine andere Klasse in meinem Modell erstellt und bin an diesem Problem vorbeigekommen. – George