mar*_*zzz 0 c# serialization json dictionary
这是我的代码:
public class PuntoMappa
{
string Lat;
string Lng;
public PuntoMappa(string Lat, string Lng)
{
this.Lat = Lat;
this.Lng = Lng;
}
}
PuntiCategoriaMappa.Add("1111", new PuntoMappa("1", "2"));
PuntiCategoriaMappa.Add("2222", new PuntoMappa("3", "4"));
PuntiCategoriaMappa.Add("3333", new PuntoMappa("5", "6"));
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "PuntiCategoriaMappa", "PuntiCategoriaMappa = " + jsonSerializer.Serialize(PuntiCategoriaMappa) + ";", true);
Run Code Online (Sandbox Code Playgroud)
但序列化是:
PuntiCategoriaMappa = {"1111":{},"2222":{},"3333":{}};
Run Code Online (Sandbox Code Playgroud)
好吧,我丢失了PuntoMappa对象的序列化.
我怎样才能正确地做到这一点?
您必须使Lat和Lng可公开访问.
public class PuntoMappa
{
public string Lat { get; private set; }
public string Lng { get; private set; }
public PuntoMappa(string Lat, string Lng)
{
this.Lat = Lat;
this.Lng = Lng;
}
}
Run Code Online (Sandbox Code Playgroud)