C#保存到动态对象
我已将以下内容转换为动态对象。以下是对象的外观:C#保存到动态对象,c#,dynamic,C#,Dynamic,我已将以下内容转换为动态对象。以下是对象的外观: "rejectionReason": null, "timestamp": { "testDate1": null, "testDate2": null, "testDate3": null, "testDate4": null, "testDate5": "2016-03-23T15:41:22.000-06:00", "testDate6": "2016-03-23T15:36:32.000-06:00", "te
"rejectionReason": null,
"timestamp": {
"testDate1": null,
"testDate2": null,
"testDate3": null,
"testDate4": null,
"testDate5": "2016-03-23T15:41:22.000-06:00",
"testDate6": "2016-03-23T15:36:32.000-06:00",
"testDate7": "2016-03-23T15:39:22.000-06:00"
},
"outsideTestDate": null,
我可以使用以下工具轻松保存到外部测试日期
:
dynamicObj.outsideTestDate = DateTimeOffset.Now;
但是,我试图保存到testDate1
-testDate4
,但似乎无法使其正常工作,每次都返回null,并且似乎位于表达式的左侧。我试过:
dynamicObj.timestamp.testDate4 = DateTimeOffset.Now
有什么想法可以为我指明正确的方向吗?这不是一个很好的解决方案,但下面是我为让它发挥作用所做的
DateTimeOffset td5 = DateTimeOffset.Parse(inspection.timestamp.testDate5.ToString());
DateTimeOffset td6 = DateTimeOffset.Parse(inspection.timestamp.testDate6.ToString());
DateTimeOffset td7 = DateTimeOffset.Parse(inspection.timestamp.testDate7.ToString());
dynamicObj.timestamp = new
{
testDate1 = (DateTimeOffset?)null,
testDate2 = (DateTimeOffset?)null,
testDate3 = (DateTimeOffset?)null,
testDate4 = DateTimeOffset.Now,
testDate5 = td5,
testDate6 = td6,
testDate7 = td7
};
您是将json转换为c#object还是反过来转换?如何转换为
动态
?如何检查null
?我使用的是Json.NET
,这对我来说很好:dynamic data=JObject.Parse(Json);data.timestamp.testDate4=DateTimeOffset.Now代码>我想我应该澄清一下,我是从azure文档数据库中提取的,所以我正在将json转换为C#