2012-09-27 51 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; 
    } 
} 
相關問題