2012-01-26 147 views
1

我傳遞(或很好地嘗試!)一個JSON對象到我的代碼隱藏。JSON.NET反序列化多維數組?

這是JSON對象:

[ 
    { 
     "testLaag":{ 
      "layer_zindex":1, 
      "layer_type":1, 
      "layer_width":1, 
      "layer_height":1, 
      "layer_offset_left":1, 
      "layer_offset_top":1, 
      "layer_html":1, 
      "layer_fontcolor":1, 
      "layer_fontsize":1, 
      "layer_rotation":1, 
      "layer_color":1, 
      "layer_name":1, 
      "layer_fontFamily":1 
     }, 
     "testLaag2":{ 
      "layer_zindex":2, 
      "layer_type":2, 
      "layer_width":2, 
      "layer_height":2, 
      "layer_offset_left":2, 
      "layer_offset_top":2, 
      "layer_html":2, 
      "layer_fontcolor":2, 
      "layer_fontsize":2, 
      "layer_rotation":2, 
      "layer_color":2, 
      "layer_name":2, 
      "layer_fontFamily":2 
     } 
    } 
] 

從後面的代碼的代碼:

string data = "[" + Request.Form["layers"] + "]"; 
JsonSerializerSettings settings = new JsonSerializerSettings(); 
settings.TypeNameHandling = TypeNameHandling.All; 
List<Laag> layers = JsonConvert.DeserializeObject<List<Laag>>(data, settings); 

foreach (Laag laag in layers) 
{ 
    Response.Write(laag.layer_name); 
} 

這裏是我的課:

public class Laag 
{ 
    public int layer_zindex {get; set;} 
    public int layer_type { get; set; }  
    public double layer_width { get; set; } 
    public double layer_height { get; set; } 
    public double layer_offset_left { get; set; } 
    public double layer_offset_top { get; set; } 
    public string layer_html { get; set; } 
    public string layer_fontcolor { get; set; } 
    public double layer_fontsize { get; set; } 
    public double layer_rotation { get; set; } 
    public string layer_color { get; set; } 
    public string layer_name { get; set; } 
    public string layer_fontFamily { get; set; } 
} 

這是目前驅動兩個我和我的同事堅果。我們想要的是在List對象中都有「laag」對象。 JSON以及如何處理對我們來說都是相當新的,所以我們可能會錯過一些令人難以置信的愚蠢的東西。

由於提前,

-Ferdy

回答

1

創建另一個包裝類

public class wrapperOfLaag{ 
    public List<Laag> listLag; 
} 

,然後嘗試

string data = "[" + Request.Form["layers"] + "]"; 
JavaScriptSerializer ser = new JavaScriptSerializer(); 
wrapperOfLaag p = ser.Deserialize<wrapperOfLaag >(data);