我正在試圖爲jsonstream.Length扔類型System.NotSupportedException的異常 錯誤:無法分配HttpWebResponse.GetResponseStream()到流
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);
}
還與Web客戶端嘗試,但仍然是相同的錯誤。 這是因爲響應大嗎?
請求URL是: 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; }
}
謝謝,但你可以請LINQ表達式幫助獲得敢,我試過但得到錯誤無法找到源類型對象的查詢模式的實現。選擇未找到,更新了問題 – Simsons
@Simsons我更新了答案。 –