Click here to Skip to main content
15,902,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://xml.flightview.com/fvDemoConsOOOI/fvxml.exe?a=fvxmldemoSoo1&b=thrk$xxxx&depap=xxx&depdate=xxxxxx&dephr=xxxx");
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json; charset=utf-8";
string file;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
file = sr.ReadToEnd();
}
//var json = JObject.Parse(file);
var table = JsonConvert.DeserializeAnonymousType(file, new { Makes = default(DataTable) }).Makes;
if (table.Rows.Count > 0)
{
//do something
}


What I have tried:

I am trying to do this code but i am getting exception -

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.


so kindely please help me urgent
Posted
Updated 1-Dec-18 16:37pm
v2
Comments
F-ES Sitecore 30-Nov-18 12:15pm    
From your other questions on this it looks like your web method is not returning json but either html or xml. That would explain why the first item in your data is "<" rather than "[" or "{" as you would expect with json.

If your web method returns xml then use an xml deserialiser.
Graeme_Grant 30-Nov-18 21:31pm    
Have you tried setting a breakpoint on this line?
var json = JObject.Parse(file);

Now look at the contents of the variable file in the locals window ... it is called "debugging".

1 solution

Your issue is that you are trying to treat an XML response as if it were JSON (not sure if you realized it was xml is why i point this out).

Pull up this URL in your browser: http://xml.flightview.com/fvDemoConsOOOI/fvxml.exe?a=fvxmldemoSoo1&b=thrk$xxxx&depap=xxx&depdate=xxxxxx&dephr=xxxx[^]

So what you need to do is first convert the XML to JSON, then handle the JSON when it comes back.

To do that you need to use JsonConvert.SerializeXmlNode() method. In your case you probably need an intermediary step that looks something like

C#
var xmldoc = new XmlDocument();
xmldoc.LoadXml(file);
var json = JsonConvert.SerializeXmlNode(xmldoc);
var table = JsonConvert.DeserializeAnonymousType(json, new { Makes = default(DataTable) }).Makes;


From there its up to you to do with the json whatever it is you need to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900