Rajib Arefin

Rajib Arefin

  • NA
  • 47
  • 3.4k

Json object converting error at the time of deserializing

Jul 1 2018 4:08 AM
Assalamualaikum Sir,
 
This is Leon from Bangladesh.
 
I getting many help from your tutorial. A lot of thanks for that. Sir, here I am facing a problem when I m going to retrieve data from SQL Server using WEB Api and Xamarin Form.
 
I can add data in SQL Server following your FEEDBACK tutorial in Youtube. But my GET method is not working properly, can u help me about that?? The Error is following given below:
 
"Error converting value \"{\"ID\":5,\"EMail\":\"leonpowerpoint\",\"Subject\":\"The Sbj\",\"Messege\":\"Hellow Bro\"}\" to type 'FEEDBACK_APP.Feedback'. Path '', line 1, position 92."
 
MY CODE IS:
 
Feedback fb1 = null;
HttpClient client = new HttpClient();
string Url = $"http://10.0.0.10:8083/api/Feedback?ID=" + "5".ToString();
var result = await client.GetAsync(Url);
var json = await result.Content.ReadAsStringAsync();
var uri = new Uri(Url);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
try
{
fb1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Feedback>(json);
//HERE GETTING ERROR
}
catch(Exception r)
{
string st = r.Message;
await DisplayAlert("Title", r.Message, "OK");
}
if (fb1 == null)
{
}
else
{
//tx = fb1.EMail;
await DisplayAlert("Title", fb1.EMail, "OK");
}
public class Feedback
{
public string ID { get; set; }
public string EMail { get; set; }
public string Subject { get; set; }
public string Messege { get; set; }
}
WEB API:
public IHttpActionResult Get_Feedback(int ID)
{
User user = null;
try
{
user = db.Users.ToList().Where((u) => { return u.ID == ID; }).FirstOrDefault();
}
catch (Exception r)
{
Debug.WriteLine(r.Message);
}
if (user == null)
{
return Ok("No record Found");
}
else
{
var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
return Json(json);
}
}
DATABASE:
USE [fdb]
GO
/****** Object: Table [dbo].[User] Script Date: 01/07/2018 3:06 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[User](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EMail] [varchar](100) NOT NULL,
[Subject] [varchar](100) NOT NULL,
[Messege] [varchar](200) NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_Feedback_EMail] DEFAULT ('') FOR [EMail]
GO
ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_Feedback_Subject] DEFAULT ('') FOR [Subject]
GO
ALTER TABLE [dbo].[User] ADD CONSTRAINT [DF_Feedback_Messege] DEFAULT ('') FOR [Messege]
GO

Answers (3)