Я пытаюсь десериализовать объект JSON и хотел бы использовать его имя свойства в качестве атрибута, но не знаю, как это сделать.
Вот мои классы модели:
public partial class Questions
{
[JsonProperty("timestamp")]
public DateTimeOffset TimeStamp { get; set; }
[JsonProperty("choices")]
public Choices choices { get; set; }
}
public partial class Choices
{
[JsonProperty("choices")]
public string[] choices { get; set; }
}
Вот связанный JSON:
{
"id": "5e6106600066d227a231ceb8",
"complete": null,
"questions": {
"5e60af61a7be775b0d31ea77": {
"timeStamp": "2020-03-05T15:01:56.000000Z",
"choices": [
"dsbb"
]
},
"5e60af66a7be775b0d31ea78": {
"timeStamp": "2020-03-05T15:02:02.000000Z",
"choices": [
"9999999999"
]
},
"5e60af76dd15333d1727ce09": {
"timeStamp": "2020-03-05T15:02:11.000000Z",
"choices": [
"lj@test.com"
]
},
"5e60afeeb406ed608058d045": {
"timeStamp": "2020-03-05T15:02:15.000000Z",
"choices": [
0
]
},
"5e5d282331808f44ce4b0b76": {
"timeStamp": "2020-03-05T15:02:22.000000Z",
"choices": [
0
]
},
"5e5cec17ae23a40b0c645614": {
"timeStamp": "2020-03-05T15:02:29.000000Z",
"choices": [
0
]
},
"5e5d08d235bf95782b049cb3": {
"timeStamp": "2020-03-05T15:02:34.000000Z",
"choices": [
2
]
},
"5e5d0a05a0be6b6533195f17": {
"timeStamp": "2020-03-05T15:02:43.000000Z",
"choices": [
0
]
},
"5e5cecdcf3c27f611b3df2fa": {
"timeStamp": "2020-03-05T15:03:01.000000Z",
"choices": [
"100"
]
},
"5e5cedd7949da059190f2146": {
"timeStamp": "2020-03-05T15:03:10.000000Z",
"choices": [
1,
3,
4
]
},
"5e60e8e899017615e27ad107": {
"timeStamp": "2020-03-05T15:03:15.000000Z",
"choices": [
0
]
},
"5e60e95d479b812cb4777b2f": {
"timeStamp": "2020-03-05T15:03:22.000000Z",
"choices": [
0
]
},
"5e60e9feff05631d3b0585d8": {
"timeStamp": "2020-03-05T15:03:59.000000Z",
"choices": [
"fveg"
]
}
},
"ip_address": "188.165.111.130",
"created_at": "2020-03-05T14:02:08.621000Z",
"updated_at": "2020-03-05T14:04:21.995000Z"
}
И, наконец, мой код привязки:
JObject respondants= JObject.Parse(json);
IList<JToken> questions = new List<JToken>();
questions = respondants["questions"].Children().ToList();
foreach(JToken q in questions){
Questions question = q.ToObject<Questions>();
}
Пытаясь сделать это, я сталкиваюсь со следующим исключение:
Newtonsoft. Json .JsonSerializationException 's'est produite dans System.Private.CoreLib.dll:' Неожиданный токен при десериализации объекта: PropertyName
Любой идея?