I have my code that reads an external API and returns me information, I would like to read this information or add it to a class:
I'm using Newtonsoft.Json;
This is Json:
{
"success": true,
"errorMessage": null,
"answer": {
"token": "8686330657058660259"
}
}
public class usuario
{
public string success { get; set; }
public string errorMessage { get; set; }
public List<String> answer { get; set; }
}
public string ConsultaUsuario(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
username = "sistemacdm",
password = "qZrm4Rqk"
});
streamWriter.Write(json);
}
var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
string json = streamReader.ReadToEnd();
usuario m = JsonConvert.DeserializeObject<usuario>(json);
string name = m.success;
return streamReader.ReadToEnd();
}
}
Error:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll, but was not processed in user
Additional information: Unable to deserialize the JSON object current (for example, {"name": "value"}) in type 'System.Collections.Generic.List'1 [System.String]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.
To correct this error, change the JSON to a JSON array (for example, [1,2,3]) or change the deserialized type so that it is a type Normal .NET (for example, it is not a primitive type as an integer, nor a type of collection as an array or list) that can be deserialized from a JSON object. JsonObjectAttribute too can be added to the type to force it to deserialize from a JSON object.
Path 'answer.token', line 1, position 54.