After hours of searching and trying, can someone please be so kind and help me solving this following simple problem:
I have the following JSON-String:
[
{
"key": 1234,
},
{
"key": 9876,
}
]
How can I read this JSON and write all values into a List?
Had many attempts so far, but please see following code:
List<int> content = new List<int>;
var json = reader.ReadToEnd();
var obj = JObject.Parse(json);
First try:
foreach(var key in obj)
{
content.Add((int)obj["key"]);
}
Other try:
var token = obj.SelectToken("key");
foreach(var item in token)
{
content.Add(JsonConvert.DeserializeObject<int>(item.value));
}
Or something this way?
foreach(var key in obj)
{
content.Add(Int32.Parse(obj.GetValue("key").ToString()));
}
Trying to run the last attempt, I get following error message:
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray.
Even if the JSON looks like the following:
[{\"key\":9999},{\"key\":9876}]
Would be very happy for every answer.
Best regards