2

This code generates an exception of unexpected character. What is the problem with this code? Error is: 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: S. Path '', line 0, position 0.

HttpClient http = new System.Net.Http.HttpClient(); http.DefaultRequestHeaders.Add("accept", "Application/JSON"); var page = http.GetStringAsync(searchUrl); var o = (JObject)JsonConvert.DeserializeObject(page.ToString()); 
1
  • type of searchUrl is uri. Commented Feb 8, 2014 at 20:50

1 Answer 1

3

HttpClient.GetStringAsync returns a Task<string>.

You don't wait for it to complete, instead you call .ToString() on it, which is likely to either:

  • Return the full name of the Task<T> type
  • Return some internal string representation of the Task<T> type (unlikely)

As such, what you have is not the Json document, but the task string, which you then try to deserialize Json from.

Try:

string page = await http.GetStringAsync(searchUrl); 

or:

string page = http.GetString(searchUrl).Result; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.