C# Json.NET - json to c# object



我有以下json,我正试图使用json.net.将其转换为C#对象

{
    "torrents": [
        [
            "03FE02E76D99D94B9FF7689080F168C70BC2A3B7",
            136, "02 - Aleph One.mp3", 98206320,
            0, 0, 0, 0, 0, 0, 0, "",
            0, 0, 0, 0, 0, 2, 98206320,
            "", "", "Stopped 0.0 %", "4",
            1427414173, 0, "", "D:\downloading",
            0, "481B6500"
        ],
        [
            "6D5A14A99970EF0366F69BF21BD6633D19F8C80C",
             128, "01 - Infinity Mix 1.mp3", 279245285,
             0, 212992, 0, 0, 0, 0, 0, "",
             0, 0, 0, 0, 0, 1, 279032293,
             "", "", "Stopped 0.0 %", "3",
             1427414139, 0, "", "D:\downloading",
             0, "C32DD134"
        ]
    ]
}

我有以下代码,我不确定为什么它不起作用,也许有人可以给我指明正确的方向。

我得到的错误消息是:"对象引用没有设置为对象的实例。"

var ds = JsonConvert.DeserializeObject<TorrentList>(json);
MessageBox.Show(ds.Torrents[0].Name);

public class TorrentList
{
    public Torrent[] Torrents { get; set; }
}
[JsonConverter(typeof (TorrentJsonConverter))]
public class Torrent
{
    public String Hash { get; set; }
    public int Status { get; set; }
    public String Name { get; set; }
    public int Size { get; set; }
    public int Progress { get; set; }
    public int Downloaded { get; set; }
    public int Uploaded { get; set; }
    public int Ratio { get; set; }
    public int UploadSpeed { get; set; }
    public int DownloadSpeed { get; set; }
    public int Eta { get; set; }
    public String Label { get; set; }
    public int PeersConnected { get; set; }
    public int PeersInSwarm { get; set; }
    public int SeedsConnected { get; set; }
    public int SeedsInSwarm { get; set; }
    public int Availability { get; set; }
    public int TorrentOrder { get; set; }
    public int Remaining { get; set; }
    public String Unknown1 { get; set; }
    public String Unknown2 { get; set; }
    public String CurrentStatus { get; set; }
    public String Unknown3 { get; set; }
    public int Unknown4 { get; set; }
    public int Unknown5 { get; set; }
    public String Unknown6 { get; set; }
    public String FilePath { get; set; }
    public int Unknown7 { get; set; }
    public String Unknown8 { get; set; }
}
internal class TorrentJsonConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var obj = new Torrent
        {
            Hash = reader.ReadAsString(),
            Status = reader.ReadAsInt32().GetValueOrDefault(),
            Name = reader.ReadAsString(),
            Size = reader.ReadAsInt32().GetValueOrDefault(),
            Progress = reader.ReadAsInt32().GetValueOrDefault(),
            Downloaded = reader.ReadAsInt32().GetValueOrDefault(),
            Uploaded = reader.ReadAsInt32().GetValueOrDefault(),
            Ratio = reader.ReadAsInt32().GetValueOrDefault(),
            UploadSpeed = reader.ReadAsInt32().GetValueOrDefault(),
            DownloadSpeed = reader.ReadAsInt32().GetValueOrDefault(),
            Eta = reader.ReadAsInt32().GetValueOrDefault(),
            Label = reader.ReadAsString(),
            PeersConnected = reader.ReadAsInt32().GetValueOrDefault(),
            PeersInSwarm = reader.ReadAsInt32().GetValueOrDefault(),
            SeedsConnected = reader.ReadAsInt32().GetValueOrDefault(),
            SeedsInSwarm = reader.ReadAsInt32().GetValueOrDefault(),
            Availability = reader.ReadAsInt32().GetValueOrDefault(),
            TorrentOrder = reader.ReadAsInt32().GetValueOrDefault(),
            Remaining = reader.ReadAsInt32().GetValueOrDefault(),
            Unknown1 = reader.ReadAsString(),
            Unknown2 = reader.ReadAsString(),
            CurrentStatus = reader.ReadAsString(),
            Unknown3 = reader.ReadAsString(),
            Unknown4 = reader.ReadAsInt32().GetValueOrDefault(),
            Unknown5 = reader.ReadAsInt32().GetValueOrDefault(),
            Unknown6 = reader.ReadAsString(),
            FilePath = reader.ReadAsString(),
            Unknown7 = reader.ReadAsInt32().GetValueOrDefault(),
            Unknown8 = reader.ReadAsString()
        };
        reader.Read();
        return obj;
    }

在此处添加一个空检查,可能您的项目为空,

if(ds != null)
{
   if(ds.count > 0 )
    { 
      if(ds.Torrents.count > 0)
       {
      MessageBox.Show(ds.Torrents[0].Name);
       }
   }
}

相关内容

  • 没有找到相关文章

最新更新