My webapi method is:
public JsonResult<List<MyClass>> PullData()
{
List<MyClass> data = new List<MyClass>();
data = db.TableName.Select(x => new MyClass
{
Id = x.Id,
IsActive = x.IsActive,
//other attribute..
}).ToList();
return Json(data);
}
And I'm consuming this webapi as:
public async Task<string> Index()
{
string apiUrl = "http://localhost:90/api/Scheduler/pulldata";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
JsonConvert.DeserializeXmlNode(data, "root"); //exception: XmlNodeConverter can only convert JSON that begins with an object.
}
}
return "Error";
}
I get error:
XmlNodeConverter can only convert JSON that begins with an object.
Also In api consumption method (i.e, Index
) when I debug and see data in var data = await response.Content.ReadAsStringAsync();
as JSON Visualizer
it shows data fine.
But when I do XML visualizer, it doesn't show data.
Update:
The data is too large. I can't share it. Here is the screen shot of data.
Update 2:
Here is a part of json data from begining:
[{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":1.0,"ModuleDescription":"<p><span style="background-color:rgb(240, 240, 240); font-family:archivo narrow,helvetica,arial,sans-serif; font-size:16px; line-height:20px; white-space:pre-line">Learn SAP
Update 3:
I have changed the webapi method PullData()
to send only two records, so that we can easily visualize wethere the issue is with json data.
Complete data is:
[{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":0.0,"ModuleDescription":null,"BadgeName":null,"BadgeVersion":null,"BadgeDescription":null,"MozillaBadge":null,"LearningActivityName":null,"LearningActivityDescription":null,"StepName":null,"StepVersion":null,"StepDescription":null,"IsActive":false,"IsPublished":false,"CreatedDate":"0001-01-01T00:00:00","ModifiedDate":null},{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":0.0,"ModuleDescription":null,"BadgeName":null,"BadgeVersion":null,"BadgeDescription":null,"MozillaBadge":null,"LearningActivityName":null,"LearningActivityDescription":null,"StepName":null,"StepVersion":null,"StepDescription":null,"IsActive":false,"IsPublished":false,"CreatedDate":"0001-01-01T00:00:00","ModifiedDate":null}]
I pasted data in https://jsonformatter.curiousconcept.com/ and it says:
And XML Visualizer
still doesn't show any data.
See Question&Answers more detail:
os