I am getting the JSON request from http://www.viki.com/api/v2/channels.json
Now, I have the following classes from JSON:
public class RootObject
{
public int id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string uri { get; set; }
public List<Episode> episodes { get; set; }
public Thumbnails2 thumbnails { get; set; }
public string timestamp { get; set; }
public List<object> genres { get; set; }
public string origin_code { get; set; }
}
public class Thumbnails2
{
public string c_220_160 { get; set; }
public string c_102_102 { get; set; }
public string c_180_130 { get; set; }
public string c_110_80 { get; set; }
public string xl { get; set; }
public string large { get; set; }
public string medium { get; set; }
public string small { get; set; }
public string c_320_300 { get; set; }
public string c_640_600 { get; set; }
public string c_95_70 { get; set; }
public string c_190_140 { get; set; }
public string c_280_200 { get; set; }
public string c_560_400 { get; set; }
}
Now I want to get the Values from large and medium in Thumbnails2 and all the episodes in List.
I tried with:
using (var wc = new WebClient())
{
var json = wc.DownloadString(jsonRequestURL);
dynamic vikiDesrialized = JsonConvert.DeserializeObject(json);
foreach (var item in vikiDesrialized)
{
//Console.WriteLine(item.title);
foreach (var thumnails in item.thumbnails)
{
JsonConvert.DeserializeObject(thumbNails);
}
}
}
But getting an exception which implies something else:
'The invocation of the constructor on type 'Viki.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
Can I have those values using LINQ?
You don't need the second deserialization JsonConvert.DeserializeObject(thumbNails);
either use
foreach (var item in dynObj)
{
Console.WriteLine(item.title);
Console.WriteLine(item.thumbnails.small);
}
or
using (var wc = new WebClient())
{
var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
var rootObj = JsonConvert.DeserializeObject<RootObject[]>(json);
var obj = rootObj.Select(r=>new
{
Title = r.title,
Thumbnail = r.thumbnails.small
}).ToArray();
}
Wouldn't it be an easier way to use DataContracts and use the DataContractJsonSerializer
I have used it so far only with deserialization from JSON Objects from a File, but its so easy.
Just declare the Class with the atribute [DataContract] and each member that should deserialized with [DataMember]
eg (from my code)
[DataContract]
public class CharacteristicManager
{
[DataMember(Order = 1)]
public Characteristic[] characteristics;
private Dictionary<String, Characteristic> characteristic_dictionary;
}
Deserialization looks like this:
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CharacteristicManager));
CharacteristicManager cm = (CharacteristicManager)ser.ReadObject(stream);
DataContractJsonSerializer will build the whole Object recursively including Arrays, Lists, Dictionarys.
Best Thomas
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.