【发布时间】:2016-02-22 15:50:37
【问题描述】:
我正在尝试将数据从 json 文件获取到我制作的项目对象,如下所示:
public class Project
{
public int ID { get; set; }
public int Client_ID { get; set; }
public string Name { get; set; }
public string code { get; set; }
public bool active { get; set; }
public bool billable { get; set; }
public string bill_by { get; set; }
}
我要反序列化的 json 文件在每个对象中都有 30 多个属性。我在上面制作的对象中只包含了一些。这可能是我不知道的问题?
当我这样做时:
JavaScriptSerializer ser = new JavaScriptSerializer();
List<Project> projectsList = ser.Deserialize<List<Project>>(jsonString);
JSON:
[
{
"project": {
"id": 10060601,
"client_id": 4233570,
"name": "arsnealWebsite",
"code": "",
"active": true,
"billable": true,
"bill_by": "none",
"hourly_rate": null,
"budget": null,
"budget_by": "none",
"notify_when_over_budget": false,
"over_budget_notification_percentage": 80,
"over_budget_notified_at": null,
"show_budget_to_all": false,
"created_at": "2016-02-17T18:59:22Z",
"updated_at": "2016-02-17T19:20:27Z",
"starts_on": "2016-02-19",
"ends_on": "2016-02-29",
"estimate": null,
"estimate_by": "none",
"hint_earliest_record_at": "2016-02-17",
"hint_latest_record_at": "2016-02-17",
"notes": "",
"cost_budget": null,
"cost_budget_include_expenses": false
}
}
]
...该列表是使用 json 文件具有的确切数量的对象创建的,但 json 文件中的属性没有进入我创建的项目对象的属性中? json文件中的属性名称和c#代码中的属性名称(上面的类)是完全一样的吗?为什么属性值没有进入项目对象的属性值?
【问题讨论】:
-
而 jsonString 的值看起来像...?
-
您是否尝试反序列化单个对象?
-
请显示minimal reproducible example。当我们可以重现问题时,为您提供帮助会容易得多。
-
@Robert Haile,请发布 JSON 数据的示例。如果它包含敏感信息,当然首先清除它。除非您提供此信息,否则很难提供帮助。
-
jsonstring 很长向下滚动并查看@OndrejSvejdar
标签: c# json json-deserialization