Hello,
I feel this is an incredibly easy issue to resolve, but I just can't seem to get my code to work!
All I am trying to do is loop through a JSON Object and publish relevant Object items onto my MVC web page.
Here is my code below (I've kept this very basic just so I can get the basics correct first time round)
However, I keep getting the following error: Newtonsoft.Json.Linq.JProperty' does not contain a definition for 'alertName''.
I'm aware that I'm currently not using the Model properties, however, in other attempts I've tried I have been.
Any help is greatly appreciated.
What I have tried:
Controller Code:
var jsonClient = new WebClient();
jsonClient.Credentials = new NetworkCredential("XXXX", "XXXX");
jsonClient.Headers.Set("Content-Type", "application/json");
jsonClient.Headers.Set("Accept", "application/json");
var alertsSettings = jsonClient.DownloadString("http://XXXXX:XXXX/api/alerts/settings");
dynamic alertObj = JsonConvert.DeserializeObject(alertsSettings);
foreach (var item in alertObj)
{
Viewbag.Test123 = item.alertName;
}
Model Code:
public class AlertObject
{
public string alertName { get; set; }
public string value { get; set; }
public string alertMnemonic { get; set; }
public string enabled { get; set; }
public string unit { get; set; }
public string warning { get; set; }
public string sharedParam { get; set; }
public string type { get; set; }
public string id { get; set; }
}
This is the JSON:
{
"data": [
{
"id": 268500993,
"alertName": "High CPU Usage",
"alertMnemonic": "HIGH_CPU_USAGE",
"enabled": true,
"parameters": {
"alarm": {
"unit": "MINUTES",
"value": 15
},
"warning": {
"unit": "MINUTES",
"value": 5
},
"sharedParam": {
"unit": "CPU",
"value": 70,
"name": "CPU Usage"
},
"type": "TERNARY"
}
},
{
"id": 134479876,
"alertName": "Custom Module Changed",
"alertMnemonic": "CUSTOM_MODULE_CHANGED",
"enabled": false,
"parameters": null
},
{
"id": 268500992,
"alertName": "Low JVM Memory",
"alertMnemonic": "LOW_AVAILABLE_JVM_MEMORY",
"enabled": true,
"parameters": {
"alarm": {
"unit": "MEMORY",
"value": 64
},
"warning": {
"unit": "MEMORY",
"value": 128
},
"sharedParam": {
"unit": "MINUTES",
"value": 5,
"name": "Time"
},
"type": "TERNARY"
}
}
],
"error": null
}