2012-09-27 53 views
0

这是我的代码:如何序列化字典<字符串,对象>为JavaScript?

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); 

但串行化:

PuntiCategoriaMappa = {"1111":{},"2222":{},"3333":{}}; 

好吧,我输了PuntoMappa对象的序列化。

我该如何正确地做到这一点?

回答

5

您必须公开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; 
    } 
} 
相关问题