【问题标题】:c# json List deserializationc# json列表反序列化
【发布时间】:2015-12-24 15:22:28
【问题描述】:

有以下课程:

[DataContract]
public class TestDynamicJSON1
{
    public TestDynamicJSON1()
    {
        this.TO2_List = new List<TestDynamicJSON2>();
    }

    [DataMember]
    public int A { get; set; }

    [DataMember]
    public string B { get; set; }

    [DataMember]
    public DateTime C { get; set; }

    [DataMember]
    public List<TestDynamicJSON2> TO2_List { get; set; }
}

[DataContract]
public class TestDynamicJSON2
{
    public TestDynamicJSON2()
    {

    }

    [DataMember]
    public int A2 { get; set; }

    [DataMember]
    public string B2 { get; set; }

    [DataMember]
    public DateTime C2 { get; set; }
}

[DataContract]
public class TestDynamicJSON2_1 : TestDynamicJSON2
{
    public TestDynamicJSON2_1()
    {

    }

}

[DataContract]
public class TestDynamicJSON2_2 : TestDynamicJSON2
{
    public TestDynamicJSON2_2()
    {

    }
}

[DataContract]
public class TestDynamicJSON2_3 : TestDynamicJSON2
{
    public TestDynamicJSON2_3()
    {

    }
}

在服务器端将对象转换为 JObject:

public JObject GetDynamicJO()
        {
            TestDynamicJSON1 TO = new Metadata.CatalogManagement.TestDynamicJSON1();

            TO.A = 1;
            TO.B = "2";
            TO.C = DateTime.Now;

            TestDynamicJSON2_1 TO2_1 = new Metadata.CatalogManagement.TestDynamicJSON2_1();

            TO2_1.A2 = 11;
            TO2_1.B2 = "22";
            TO2_1.C2 = DateTime.Now;

            TO.TO2_List.Add(TO2_1);

            TestDynamicJSON2_2 TO2_2 = new Metadata.CatalogManagement.TestDynamicJSON2_2();

            TO2_2.A2 = 11;
            TO2_2.B2 = "22";
            TO2_2.C2 = DateTime.Now;

            TO.TO2_List.Add(TO2_2);


            TestDynamicJSON2_3 TO2_3 = new Metadata.CatalogManagement.TestDynamicJSON2_3();

            TO2_3.A2 = 11;
            TO2_3.B2 = "22";
            TO2_3.C2 = DateTime.Now;

            TO.TO2_List.Add(TO2_3);


            JProperty property1 = new JProperty("DO", JToken.FromObject(TO));
            JObject jo = new JObject(property1);

            return jo;
        }

在客户端尝试反序列化:

var str = await response1.Content.ReadAsStringAsync();

var jo = JsonConvert.DeserializeObject<TestDynamicJSON1>(str);

Json 字符串如下:

{"DO":{"A":1,"B":"2","C":"2015-12-24T19:42:51.6509893+04:00","TO2_List":[{"A2":11,"B2":"22","C2":"2015-12-24T19:42:51.6509893+04:00"},{"A2":11,"B2":"22","C2":"2015-12-24T19:42:51.6509893+04:00"},{"A2":11,"B2":"22","C2":"2015-12-24T19:42:51.6509893+04:00"}]}}

并且列表 TO2_List 属性为空。是不是因为它是 BaseClass 的列表?

你能帮忙吗?

谢谢。

【问题讨论】:

  • 你的 json 字符串是什么样的?
  • 对不起,我更新了问题,实际上问题是 TO2_List 是属性为空。实际上应该有 3 个对象。
  • 那些类名非常混乱。
  • 请阅读How to Ask 并提供minimal reproducible example。您的问题中没有 JSON,因此我们无法验证发生了什么。
  • 感谢您的 cmets。由于我是 WebApi 和 Json 的初学者,我的类名仅用于测试目的。再次感谢。

标签: c# json inheritance deserialization


【解决方案1】:

你的班级应该是这样的。

public class TO2List
{
     public int A2 { get; set; }
     public string B2 { get; set; }
     public string C2 { get; set; }
}

public class DO
{
     public int A { get; set; }
     public string B { get; set; }
     public string C { get; set; }
     public List<TO2List> TO2_List { get; set; }
}

public class RootObject
{
    public DO DO { get; set; }
}

你以这种方式绑定值。

var str = await response1.Content.ReadAsStringAsync();

var jo = JsonConvert.DeserializeObject<RootObject>(str);

这样你就能获得价值

//jo.DO.A

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多