travy92 Posted December 28, 2011 Report Share Posted December 28, 2011 Hey guys, I've successfully connected, authenticated, retrieved a token and requested /gui/?list=1 of torrents. What I'm stuck on is how to deal with the elements in the retrieved JSON that are LISTS.ie. "labels" and "torrents" are of type list while "build" is of type string.My code: private void client_ReadCompleted(object sender, OpenReadCompletedEventArgs e) { try { //show results (should be list of torrents etc) //open stream to the list of torrents in JSON form Stream list = (Stream)e.Result; list.Position = 0; StreamReader read = new StreamReader(list); String content = read.ReadToEnd(); var ser = new DataContractJsonSerializer(typeof(webAPIJSON)); webAPIJSON obj = ser.ReadObject(list) as webAPIJSON; //deserialze stream MessageBox.Show(obj.build); MessageBox.Show(obj.label); MessageBox.Show(obj.torrents.ToString()); read.Close(); } [DataContract] public class webAPIJSON { [DataMember] public string build { get; set; } [DataMember] public string torrents { get; set; } [DataMember] public string label { get; set; } }So when I want to view obj.torrents.ToString() (or label), it simply returns "System.Collections.Generic.List`1".I tried changing the return type of torrents in webAPIJSON to "public List<..> torrents" but then I am unable to deserialize the JSON as it returns an error with ReadObject..Thanks. Link to comment Share on other sites More sharing options...
travy92 Posted December 28, 2011 Author Report Share Posted December 28, 2011 EDITFixed the problem myself.For anyone having the same problem, basically I declared a double string list in the webAPIJSON class instead of a normal string: [DataMember] public List<List<String>> torrents { get; set; }and then using this, from before I can do:foreach (List<String> torrent in obj.torrents) //show each torrent information { foreach (String info in torrent) { MessageBox.Show(info); } }To basically get all the information.Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.