C# deserialize Json based on condition -


{     "timeago": "6 minutes ago",     "time": "07/11/2016 07:00 am",     "alertid": 145928,     "details": {       },     "priority": 10,     "type": 2,     "isclosed": 0,     "notescount": 0,     "patientaccountid": 680,     "isread": 0   } 

i want deserialize json based on int value 'type', in such way, want details different types

public class notification {     public string timeago { get; set; }     public string time { get; set; }     public int alertid { get; set; }     public object details { get; set; }     public int priority { get; set; }     public int type { get; set; }     public int isclosed { get; set; }     public int notescount { get; set; }     public int patientaccountid { get; set; }     public int isread { get; set; } } 

if type = 1, object 'details' of type a, if type = 2, 'details' of type b , on. there 25 values type.

so, later can like:

notification n = .... if (type == 1) {     a = (a) n.details; 

if json not have appropriate typing included in json, work.

this may need tweaking if actual structure more complex, managed work on sample.

var instance = newtonsoft.json.jsonconvert.deserializeobject<notification>(     js,     new itemconverter());  public class itema : item { } public class itemb : item { } public class item { }  public class notification {     public string timeago { get; set; }     public string time { get; set; }     public int alertid { get; set; }     public item details { get; set; }     public int priority { get; set; }     public int type { get; set; }     public int isclosed { get; set; }     public int notescount { get; set; }     public int patientaccountid { get; set; }     public int isread { get; set; } }  public class itemconverter : jsonconverter {     private type currenttype;     public override bool canconvert(type objecttype)     {         return typeof(item).isassignablefrom(objecttype) || objecttype == typeof(notification);     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         jobject item = jobject.load(reader);         if (item["type"] != null)         {             // save type later.             switch (item["type"].value<int>())             {                 case 1:                     currenttype = typeof(itema);                     break;                 default:                     currenttype = typeof(itemb);                     break;             }             return item.toobject<notification>();         }          // use last type read serialise.         return item.toobject(currenttype);     }      public override void writejson(jsonwriter writer,         object value, jsonserializer serializer)     {         throw new notimplementedexception();     } } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -