我从第三方服务接收到一个JObject对象,该对象包含以下所示的JSON:
{
{
"topic": "asd",
"id": "b87a5db6-01b5-4cc0-8fa7-832382ca3431",
"eventType": "update",
"subject": "/asd/task",
"eventTime": "12/18/2018 4:46:20 PM",
"data": {
"auditId": "-9223372036854771584",
"contextData": "{}",
"eventType": null,
"auditData": {
"taskId": "22BC1515-39DF-4124-89EB-AD1468572F20",
"user": "asd@ul.com",
**"reminderDate": null,
"startDate": null,
"dueDate": null,**
"taskStatusName": "NotScheduled"
}
},
"dataVersion": "",
"metadataVersion": "1"
}
}
此JObject可能有也可能没有以粗体突出显示的JToken。
我需要查看传入的JObject中是否存在此类JToken。
如果它们存在,那么我需要从中获取价值。例如这里存在hinterDate,startDate和dueDate JTokens,因此我需要获取它们各自的值,在本示例中为null。
如果这些Jtoken在JObject中不存在,那么我需要从其他来源(例如DB)获取值。
答案 0 :(得分:0)
要获取像startDate这样的Property,它将以字符串形式返回其值;如果未找到,则返回null
string Text =
@"{
""topic"": ""asd"",
""id"": ""b87a5db6-01b5-4cc0-8fa7-832382ca3431"",
""eventType"": ""update"",
""subject"": ""/asd/task"",
""eventTime"": ""12/18/2018 4:46:20 PM"",
""data"": {
""auditId"": ""-9223372036854771584"",
""contextData"": ""{}"",
""eventType"": null,
""auditData"": {
""taskId"": ""22BC1515-39DF-4124-89EB-AD1468572F20"",
""user"": ""asd@ul.com"",
""reminderDate"": null,
""startDate"": null,
""dueDate"": null,
""taskStatusName"": ""NotScheduled""
}
},
""dataVersion"": """",
""metadataVersion"": ""1""
}";
JObject response = JObject.Parse(Text);
string startDate = (string) response ["data"]["auditData"]["startDate"];