我想迭代我正在返回的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 :(得分:1)
您可以使用newtonsoft.json来实现此目的。 下面是一个示例的链接: http://www.newtonsoft.com/json/help/html/deserializeobject.htm
您的代码应如下所示:
Assignment assignment = JsonConvert.DeserializeObject<Assignment>(jsonString);
将json解析为对象,按名称映射属性。
希望有所帮助