Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So ... I'm trying to parse out JSON into an object. It seems to work perfectly for some code, but not for others. I'm not sure what I'm doing wrong. I don't get an error, it simply does not populate.

Source Code:

C#
public AlarmQuery GetAlarms(int startDate, int endDate)
{
    //date and time are in epoch seconds.

    string url = string.Format("https://{0}/rest/v1/events?categ_filter=alarm&date_from={1}&date_to={2}&location_ids={3}", webServiceURL, startDate, endDate, "2131012");
    //string url = string.Format("https://{0}/rest/v1/events?categ_filter=alarm&location_ids={3}", webServiceURL, startDate, endDate, "2131012");

    var result = getWebResult(url);
    //result = result.Replace("object", "iobject");  //JSON uses 'object' as variable, which is reserved.  (Already replaced in sample JSON)

    var parsedLoc = JObject.Parse(result);
    AlarmQuery tmpObj = JsonConvert.DeserializeObject<AlarmQuery>(result);

    return tmpObj;
}

Source Class

C#
public class AlarmQuery
    {
        public class Meta
        {
            public int count { get; set; }
            public int total_count { get; set; }
            public double date_from { get; set; }
            public double date_to { get; set; }
        }

        public class ExtraField
        {
            public string name { get; set; }
            public string value { get; set; }
        }

        public class Attributes
        {
            public string category { get; set; }
            public int entity_id { get; set; }
            public string affected_location_ids { get; set; }
            public double timestamp { get; set; }
            public IList<ExtraField> extra_fields { get; set; }
            public string iobject { get; set; }
            public IList<object> comments { get; set; }
            public string entity { get; set; }
            public object channel_id { get; set; }
            public int num { get; set; }
            public string user { get; set; }
            public string msg { get; set; }
            public object host_id { get; set; }
            public object location_id { get; set; }
            public int? device_id { get; set; }
        }

        public class Datum
        {
            public Attributes attributes { get; set; }
            public string type { get; set; }
            public string id { get; set; }
        }

        public class Jsonapi
        {
            public string version { get; set; }
        }

        public class Links
        {
            public string next { get; set; }
            public string self { get; set; }
            public string prev { get; set; }
            public string last { get; set; }
            public string first { get; set; }
        }

        public class Example
        {
            public Meta meta { get; set; }
            public IList<Datum> data { get; set; }
            public Jsonapi jsonapi { get; set; }
            public Links links { get; set; }
        }


    }

Source JSON:

JSON
{
   "meta":{
      "count":100,
      "total_count":8848,
      "date_from":1643702146.0,
      "date_to":1644998146.0
   },
   "data":[
      {
         "attributes":{
            "category":"Alarm",
            "entity_id":2131012,
            "affected_location_ids":"2131008",
            "timestamp":1644998097.24,
            "extra_fields":[
               {
                  "name":"Error",
                  "value":"Threshold"
               },
               {
                  "name":"Location",
                  "value":"Watchdog (2131008)"
               },
               {
                  "name":"Zone",
                  "value":"System/Lyon/Building N1/LYO19501001"
               },
               {
                  "name":"Threshold Alarm Template",
                  "value":"LYO_Threshold alarm template"
               },
               {
                  "name":"Threshold",
                  "value":"Low Threshold = 0 Bool."
               },
               {
                  "name":"Host",
                  "value":"VA1-PR-SYS-O5.CR.LOCAL"
               },
               {
                  "name":"Device",
                  "value":"LYO19501001 (19501001)"
               },
               {
                  "name":"Channel",
                  "value":"Watchdog (1)"
               },
               {
                  "name":"Time on",
                  "value":"utc:1644997980"
               },
               {
                  "name":"Time off",
                  "value":"utc:1644998040"
               },
               {
                  "name":"Comments",
                  "value":"_"
               },
               {
                  "name":"Reason",
                  "value":"_"
               }
            ],
            "iobject":"Low Threshold Alarm: Location value = 0 Bool for System/Lyon/Building N1/LYO19501001/Watchdog",
            "comments":[
               
            ],
            "entity":"threshold_link",
            "channel_id":null,
            "num":1435342,
            "user":"admin (Administration super user)",
            "msg":"Alarm turned off: Threshold Alarm: LYO_Threshold alarm template for type Low Threshold value = 0 Bool for Location: System/Lyon/Building N1/LYO19501001/Watchdog.",
            "host_id":null,
            "location_id":null,
            "device_id":null
         },
         "type":"events",
         "id":"1435342"
      },
      {
         "attributes":{
            "category":"Alarm",
            "entity_id":2131012,
            "affected_location_ids":"2131008",
            "timestamp":1644998039.86,
            "extra_fields":[
               {
                  "name":"Error",
                  "value":"Threshold"
               },
               {
                  "name":"Location",
                  "value":"Watchdog (2131008)"
               },
               {
                  "name":"Zone",
                  "value":"System/Lyon/Building N1/LYO19501001"
               },
               {
                  "name":"Threshold Alarm Template",
                  "value":"LYO_Threshold alarm template"
               },
               {
                  "name":"Threshold",
                  "value":"Low Threshold = 0 Bool."
               },
               {
                  "name":"Host",
                  "value":"VA1-PR-SYS-O5.CR.LOCAL"
               },
               {
                  "name":"Device",
                  "value":"LYO19501001 (19501001)"
               },
               {
                  "name":"Channel",
                  "value":"Watchdog (1)"
               },
               {
                  "name":"Time on",
                  "value":"utc:1644997980"
               },
               {
                  "name":"Comments",
                  "value":"_"
               }
            ],
            "iobject":"Low Threshold Alarm: Location value = 0 Bool for System/Lyon/Building N1/LYO19501001/Watchdog",
            "comments":[
               
            ],
            "entity":"threshold_link",
            "channel_id":null,
            "num":1435340,
            "user":"admin (Administration super user)",
            "msg":"Alarm turned on: Threshold Alarm: LYO_Threshold alarm template for type Low Threshold value = 0 Bool for Location: System/Lyon/Building N1/LYO19501001/Watchdog. Alarm message: Watchdog.",
            "host_id":null,
            "location_id":null,
            "device_id":null
         },
         "type":"events",
         "id":"1435340"
      }
   ],
   "jsonapi":{
      "version":"5.1.5.96"
   },
   "links":{
      "next":"/rest/v1/events?page_number=2&page_size=100",
      "self":"/rest/v1/events?page_number=1&page_size=100",
      "prev":"/rest/v1/events?page_number=1&page_size=100",
      "last":"/rest/v1/events?page_number=89&page_size=100",
      "first":"/rest/v1/events?page_number=1&page_size=100"
   }
}


What I have tried:

I tried modifying the class, and I tried updating the query. The results are populated and the JOBject.parse(result) works. It seems the DeserializeObject is the hurdle at the moment.
Posted
Updated 16-Feb-22 8:18am
v2

Used to Instantly parse JSON in any language | quicktype[^] to create the class, and it seems to work better. Had to tweak it some. Tryied to make a lot of extra work.

namespace VaisalaReporter
{

    public class Test
    {
        public Meta Meta { get; set; }
        public Datum[] Datum { get; set; }
        public Jsonapi Jsonapi { get; set; }
        public Links Links { get; set; }
    }

    public partial class Datum
    {
        public Attributes Attributes { get; set; }
        public string Type { get; set; }
        public long Id { get; set; }
    }

    public partial class Attributes
    {
        public Category Category { get; set; }
        public long EntityId { get; set; }
        public string AffectedLocationIds { get; set; }
        public double Timestamp { get; set; }
        public ExtraField[] ExtraFields { get; set; }
        public string Iobject { get; set; }
        public object[] Comments { get; set; }
        public string Entity { get; set; }
        public object ChannelId { get; set; }
        public long Num { get; set; }
        public string User { get; set; }
        public string Msg { get; set; }
        public object HostId { get; set; }
        public object LocationId { get; set; }
        public long? DeviceId { get; set; }
    }

    public partial class ExtraField
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

    public partial class Jsonapi
    {
        public string Version { get; set; }
    }

    public partial class Links
    {
        public string Next { get; set; }
        public string Self { get; set; }
        public string Prev { get; set; }
        public string Last { get; set; }
        public string First { get; set; }
    }

    public partial class Meta
    {
        public long Count { get; set; }
        public long TotalCount { get; set; }
        public long DateFrom { get; set; }
        public long DateTo { get; set; }
    }
 
Share this answer
 
Check out the following article that will cover this in detail: Working with JSON in C# & VB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900