JSON.NET反序列化多维数组



我正在将一个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

创建另一个包装类

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

然后尝试

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

相关内容

  • 没有找到相关文章

最新更新