I have the following json which I am trying to convert into a C# object using json.net.
{
"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"
]
]
}
I have the following code, I am not sure why it is not working, maybe someone could point me in the right direction.
The error message I am getting is: "Object reference not set to an instance of an object."
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;
}
Add a null check here, Probably your Items are null,
if(ds != null)
{
if(ds.count > 0 )
{
if(ds.Torrents.count > 0)
{
MessageBox.Show(ds.Torrents[0].Name);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.