簡體   English   中英

如何從JSON反序列化對象列表

[英]How to deserialize list of objects from json

Thats my class.

class MyClass1 : IInterface1
{
    public MyClass1()
    {
        this.Interface2s = new List<IInterface2>();
    }

    public string strI1 { get; set; }
    public int intI1 { get; set; }
    public IList<IInterface2> Interface2s { get; set; }
    public int intC1 { get; set; }
}

在應用程序中,我使用一些隨機值對其進行了序列化。 結果:

{
  "strI1": "strI1",
  "intI1": 2,
  "Interface2s": [
    {
      "intI2": 111
    },
    {
      "intI2": 222
    },
    {
      "intI2": 333
    }
  ]
}

然后,我想反序列化該字符串,但是我正在丟失IList中的值

result of deserialization next:
{
  "strI1": "strI1",
  "intI1": 2,
  "Interface2s": [
    {
      "intI2": 0
    },
    {
      "intI2": 0
    },
    {
      "intI2": 0
    }
  ]
}

要反序列化接口,我使用的是該示例http://www.newtonsoft.com/json/help/html/DeserializeWithDependencyInjection.htm

我也需要反序列化列表中的值。 有什么建議么?

我的建議只是使用http://www.newtonsoft.com/json

JsonConvert.SerializeObject(target)

並反序列化

JsonConvert.DeSerializeObject<MyClass1>(target)

它應該就這么簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM