JSON.NET反序列化多维数组?

时间:2012-01-26 14:15:58

标签: json list multidimensional-array json.net deserialization

我正在向我的代码隐藏传递(或者试图!)一个JSON对象。

这是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 个答案:

答案 0 :(得分:1)

创建另一个包装类

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

然后尝试

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