0

为了反序列化以下 Json,我使用了以下类组合,因此我可以四次循环层次结构,但它不起作用

我还没有找到任何反序列化此类 json 的教程非常感谢任何帮助

var myFoo = new JavaScriptSerializer().Deserialize<Commn.foo2>(@"
{""root"":
    {""qss"":[
        {""id"":""d1"",""qss"":[
            {""id"":""qs1-1"",""tp"":""50px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs1-2"",""tp"":""100px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs1-3"",""tp"":""150px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs1-4"",""tp"":""200px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs1-5"",""tp"":""250px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs1-6"",""tp"":""300px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs1-7"",""tp"":""350px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs1-8"",""tp"":""400px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs1-9"",""tp"":""450px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs1-10"",""tp"":""500px"",""ck"":""c"",""dp"":""at""}]
        }

        {""id"":""d2"",""qss"":[
            {""id"":""qs2-1"",""tp"":""50px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs2-2"",""tp"":""100px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-3"",""tp"":""150px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-4"",""tp"":""200px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs2-5"",""tp"":""250px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-6"",""tp"":""300px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-7"",""tp"":""350px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-8"",""tp"":""400px"",""ck"":""i"",""dp"":""at""},
            {""id"":""qs2-9"",""tp"":""450px"",""ck"":""c"",""dp"":""at""},
            {""id"":""qs2-10"",""tp"":""500px"",""ck"":""c"",""dp"":""at""}]
        }
    }
}
");

public  class Pages
{
    public string id { get; set; }
    public qss[] children { get; set; }
}

public class qss
{
    public string id { get; set; }
    public string tp { get; set; }
    public string ck { get; set; }
    public string dp { get; set; }
}

/// <summary>
/// Holding the root elemnt
/// </summary>
public class foo2
{
    public qss[] root { get; set; }
}
4

1 回答 1

0

您使用的类不完全映射到 JSON 文件的属性。JSON 的每个对象(出现在 {} 之间的)和每个数组(出现在 [] 之间的)需要不同的类。每对,该类的成员。

public class MainObject
{
    public Root root { get; set; }
}

此类包含整个“根”值。Root 是这样定义的,因为“root”出现在 {} 之间,而“root”之后的整个字符串:是它的值。

public class Root
{
    public List<OuterQSS> qss { get; set; }
}

此类包含整个外部“qss”值,该值出现在作为数组的第二个“qss”之前。

public class OuterQSS
{
    public string id { get; set; }
    public List<InnerQSS> qss { get; set; }
}

此类包含“id”值和内部 QSS 成员的整个数组。

public class InnerQSS
{
    public string id { get; set; }
    public string tp { get; set; }
    public string ck { get; set; }
    public string dp { get; set; }
}

这个类在内部 QSS 数组中保存了所有甜美的数据。

只需替换它的类型

Deserialize<Commn.foo2> 

新主类的方法

Deserialize<MainObject>
于 2012-07-30T22:01:49.737 回答