Click here to Skip to main content
13,894,917 members
Rate this:
Please Sign up or sign in to vote.
See more:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("$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
Updated 1-Dec-18 16:37pm
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:$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

var xmldoc = new XmlDocument();
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 1 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100