簡體   English   中英

JSON.net與JsonConvert.DeserializeObject的問題

[英]JSON.net problem with JsonConvert.DeserializeObject

我有以下代碼和json:

public class Labels
{
    public Labels()
    {}

    public Label[] Label {get;set;}
}

public class Label
{
    public Label()
    { }
    public string Name { get; set; }
    public int TorrentsInLabel { get; set; }
}

//...
Labels o = JsonConvert.DeserializeObject<Labels>(json);
//...


{"label": 
[
  ["seq1",1]
  ,["seq2",2]
]}

我希望這個數組[“seq1”,“1”]反序列化為Label對象。 我錯過了什么? 一些屬性?

當我運行時,我得到異常:期望JsonArrayContract類型'test_JSONNET.Label',得到'Newtonsoft.Json.Serialization.JsonObjectContract'。

TNX

GG

JsonConvert如何知道“seq1”對應於名稱而“1”對應於TorrentsInLabel? 請查看JsonObjectAttribute,JsonPropertyAttribute,JsonArrayAttribute

默認情況下,類序列化為JSON對象,其中類上的屬性成為JSON對象上的屬性。

{
    Name: "seq",
    TorrentsInLabel: 1
}

您正在嘗試將其序列化為一個數組,而不是默認情況下Json.NET序列化程序的工作方式。

為了得到你想要的東西,你應該創建一個JsonConverter並手動讀取和寫入JSON for Label,使其成為你想要的(數組)。

暫無
暫無

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

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