I'm experiencing An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleApplication3.Program+UserData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
.
{
class Program
{
public class UserData
{
public int userId { get; set; }
public int id { get; set; }
public string title { get; set; }
public string body { get; set; }
}
static void Main(string[] args)
{
string url = @"https://jsonplaceholder.typicode.com/posts";
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
// json-formatted string from api
var responseFromServer = reader.ReadToEnd();
UserData udata = JsonConvert.DeserializeObject<UserData>(responseFromServer);//getting error connot deserialize
}
}
}