[英]How to get the value from a Complex JSON Object
I am getting the JSON request from http://www.viki.com/api/v2/channels.json 我从http://www.viki.com/api/v2/channels.json获取JSON请求
Now, I have the following classes from JSON: 现在,我有以下来自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. 现在,我想从Thumbnails2中的大中值以及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.'
'与指定绑定约束匹配的对'Viki.MainWindow'类型的构造函数的调用引发了异常。 Line number '3' and line position '9'.
行号“ 3”和行位置“ 9”。
Can I have those values using LINQ? 我可以使用LINQ获得这些值吗?
You don't need the second deserialization JsonConvert.DeserializeObject(thumbNails);
您不需要第二个反序列化
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 使用DataContracts和使用DataContractJsonSerializer会不是更简单的方法
I have used it so far only with deserialization from JSON Objects from a File, but its so easy. 到目前为止,我仅将其与来自文件的JSON对象进行反序列化一起使用,但是它是如此简单。
Just declare the Class with the atribute [DataContract] and each member that should deserialized with [DataMember] 只需使用属性[DataContract]声明类,并使用[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. DataContractJsonSerializer将以递归方式构建整个对象,包括数组,列表,字典。
Best Thomas 最佳托马斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.