Sivakumar

Sivakumar

  • NA
  • 551
  • 211.4k

exception type 'Newtonsoft.Json.JsonSerializationException

Sep 9 2015 8:12 AM
 
Hi,
 
I am consuming web api in windows form application 
 
so i implement to login api and get a access token for that .
 
but it shows an error like :
 
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'TaskAPI.LoginData[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Path 'AccessToken', line 1, position 15. 
 
public partial class Form1 : Form
{
string URI;

LoginData ld = new LoginData();

public Form1()
{
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{


}

private void button1_Click(object sender, EventArgs e)
{
URI = textBox2.Text + textBox1.Text;

GetLogin();
}
#region Methods

private async void GetLogin()
{
using (var client = new HttpClient())
{
var login = new LoginData() { Email = "[email protected]", Password="password"};
var response = await client.PostAsJsonAsync(URI, login);

if (response.IsSuccessStatusCode)
{

var productJsonString = await response.Content.ReadAsStringAsync();
dataGridView1.DataSource = JsonConvert.DeserializeObject<LoginData[]>(productJsonString).ToList();
}
}




}
}


#endregion









public class Token
{
public string AccessToken { get; set; }
public string TokenType { get; set; }
public double ExpiresIn { get; set; }
public DateTime ExpireDateTimeUtc { get; set; }
}
public class LoginData
{
public string Email { get; set; }

public string Password { get; set; }

}

}
 
How to resolve this issue.




Answers (1)