I am trying to deserialize a JSON object and would like to use its property name as an attribute, but I don't know how to do so.
Here are my model classes:
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; }
}
Here is the associated 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"
}
And finally, my binding code:
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>();
}
Trying to do so, I am facing the following exception:
Newtonsoft.Json.JsonSerializationException' s'est produite dans System.Private.CoreLib.dll: 'Unexpected token while deserializing object: PropertyName
Any idea?