Reputation: 12745
I am getting jsonstream.Length threw an exception of type System.NotSupportedException error while trying for:
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);
}
Also Tried with Webclient but still the same error. Is this because of large response size???
The request URL is: 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();
};
Edit: I tried with LINQ :
var RootObjects = from vikiroot in vikiRootObject
select new Thumbnails2
{
thumbnails = vikiRootObject.thumbnails
};
But getting the error ,Could not find an implementation of the query pattern for source type object. Select not found.
My Class Structure is something like this:
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; }
}
and
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; }
}
Upvotes: 0
Views: 1028
Reputation: 116158
Your returned json is not a single object instead it is an array. Since you haven't posted your RootObject and other child classes (I am too lazy to declare them) I will use Json.Net and dynamic
keyword
using (var wc = new WebClient())
{
var json = wc.DownloadString("http://www.viki.com/api/v2/channels.json");
dynamic dynObj = JsonConvert.DeserializeObject(json);
foreach (var item in dynObj)
{
Console.WriteLine(item.title);
foreach (var episode in item.episodes)
{
Console.WriteLine("\t" + episode.title);
}
}
}
EDIT
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
});
}
Upvotes: 2
Reputation: 8828
Try doing the following by copying it into a memory stream to be able to read the Stream:
using (Stream jsonstream = jsonHTTPResponse.GetResponseStream())
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(RootObject));
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonstream)))
{
vikiRootObject = (RootObject)serializer.ReadObject(jsonstream);
using (MemoryStream ms2 = new MemoryStream())
{
ser.WriteObject(ms2, vikiRootObject);
string serializedJson = Encoding.UTF8.GetString(ms2.GetBuffer(), 0, (int)ms2.Length);
}
}
}
Upvotes: -1
Reputation: 2750
Did you try this?
using (StreamReader reader = new StreamReader(jsonHTTPResponse.GetResponseStream()))
{
//your code goes here
}
Actually that should be in comment, but I was unable to add one.
Upvotes: 1