Reputation: 11080
I am getting the following nested Json objects from an API I am calling.
{"status":"success","data":{"valid_for":3600,"token":"access_token","expires":1123123123123}}
The PostResponse class is like below
public class PostResponse
{
public string status { get; set; }
public Data data { get; set; }
}
public class Data
{
public int valid_for { get; set; }
public string token { get; set; }
public int expires { get; set; }
}
I get null for postResponse with this code.
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
postResponse = JsonConvert.DeserializeObject<PostResponse>(reader.ReadToEnd());
}
Upvotes: 0
Views: 619
Reputation: 3900
You need to reset your stream pointer position, since you already read from a stream when you used WriteLine
method.
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
Console.WriteLine(reader.ReadToEnd());
stream.Position = 0; //Reset position pointer
reader.DiscardBufferedData();
postResponse = JsonConvert.DeserializeObject<PostResponse>(reader.ReadToEnd());
}
Upvotes: 1