javascript - How do I use JObject if the names are slightly different? -
i have jobject json.net following:
var jobject = {"schedule.id" : 1, "schedule.name" : "nameschedule"}
the above using javascript return id's , values of textboxes in mvc form in view.
in controller using c#, convert schedule object has following properties:
public class schedule { public int id {get;set;} public string name {get;set;} }
i cannot
schedule sched = jsonobject.toobject<schedule>();
because names different properties on jobject prepended 'schedule'.
is there query or way conversion allows me remove 'schedule' in jsonobject such can simple conversion in 1 line?
one simple way working use jsonproperty
attribute specify json key want map c# property:
public class schedule { [jsonproperty("schedule.id")] public int id {get;set;} [jsonproperty("schedule.name")] public string name {get;set;} }
then can use jsonconvert.deserializeobject
deserialize json schedule
instance:
var schedule = jsonconvert.deserializeobject<schedule>(json);
example: https://dotnetfiddle.net/nml9be
Comments
Post a Comment