我得到jsonstream.Length 在尝试时抛出了 System.NotSupportedException 类型的异常错误:
HttpWebRequest jsonHTTPRequest = (HttpWebRequest)WebRequest.Create(jsonRequestURL);
jsonHTTPRequest.ContentType = "application/json; charset=utf-8";
HttpWebResponse jsonHTTPResponse = (HttpWebResponse)jsonHTTPRequest.GetResponse();
RootObject vikiRootObject = default(RootObject);
using (Stream jsonstream = jsonHTTPResponse.GetResponseStream())
{
//encoding encode = system.text.encoding.getencoding("utf-8");
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(RootObject));
vikiRootObject = (RootObject)serializer.ReadObject(jsonstream);
}
也尝试了 Webclient 但仍然是同样的错误。这是因为响应量大吗???
请求网址为: http: //www.viki.com/api/v2/channels.json
WebClient webClient = new WebClient();
RootObject vikiChannelData = default(RootObject);
webClient.OpenReadAsync(new Uri(jsonRequestURL), UriKind.RelativeOrAbsolute);
webClient.OpenReadCompleted += (obj, Args) =>
{
//DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject),null);
DataContractJsonSerializer vikiChannelerialized = new DataContractJsonSerializer(typeof(RootObject));
vikiChannelData = vikiChannelerialized.ReadObject(Args.Result) as RootObject;
Console.WriteLine();
};
编辑: 我尝试使用 LINQ:
var RootObjects = from vikiroot in vikiRootObject
select new Thumbnails2
{
thumbnails = vikiRootObject.thumbnails
};
但是遇到错误,找不到源类型对象的查询模式的实现。选择未找到。
我的班级结构是这样的:
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; }
}