S.路径',0行,位置0:分析值JSON解析器异常意外遇到性格 [英] Json Parser Exception Unexpected character encountered while parsing value: S. Path '', line 0, position 0
问题描述
此代码将产生意想不到的性格异常。这有什么代码的问题吗?
错误是:类型的异常Newtonsoft.Json.JsonReaderException发生在Newtonsoft.Json.DLL但在用户代码
$ B $没有处理b更多信息:分析
值意外遇到性格:S.路径',行0,0
块引用>
HttpClient的HTTP =新System.Net.Http.HttpClient();
http.DefaultRequestHeaders.Add(接受,应用/ JSON);
VAR页= http.GetStringAsync(searchUrl);
变种O =(JObject)JsonConvert.DeserializeObject(page.ToString());
解决方案的 HttpClient.GetStringAsync 返回
任务<串GT;
。
您不要等待它完成,而不是你叫
的ToString()
就可以了,这很可能是两种:
- 返回
任务<的全名; T>
键入
- 返回内部的一些字符串表示的
任务< T>
键入(不可能的)
因此,你有什么不JSON文档,但任务的字符串,然后您再尝试从反序列化JSON。
尝试:
字符串页面=等待http.GetStringAsync(searchUrl );
或
字符串页面= http.GetString(searchUrl)。结果;
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());
解决方案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;
这篇关于S.路径',0行,位置0:分析值JSON解析器异常意外遇到性格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!