c# - Returning Empty Json Object -
i trying return json object in c#. new mvc controller , using json first time, return object, , empty.
public class { private string name; public void set(string data) { name = data; } public string get() { return name; } } public jsonresult hello() { obj = new a(); obj.set("abc"); javascriptserializer js = new javascriptserializer(); string jsonvar = js.serialize(obj); return json(jsonvar, jsonrequestbehavior.allowget); }
you assuming framework can deduce get
, set
set private variable name
's value.. doesn't.
instead, make name
public property, , should work:
public class { public string name { get; set; } } obj = new a() { name = "abc" }; /* ...etc... */
think framework's point of view. how can determine get
or set
doing? accessing same variable? knows.. runtime after all. why methods can't serialized way you're assuming.
Comments
Post a Comment