简体   繁体   English

jsonreaderexception 遇到意外字符

[英]jsonreaderexception unexpected character encountered

I have a web project using angular and C#.我有一个使用 angular 和 C# 的 Web 项目。
In a C# controller, I want to read in the contents of a local json file that is used for testing.在 C# 控制器中,我想读入用于测试的本地 json 文件的内容。

This is the code that I run to read the JSON from my working directory.这是我运行以从我的工作目录读取 JSON 的代码。

string path = HttpContext.Current.Server.MapPath("~/testing/testData.json");
JObject jsonData = JObject.Parse(path);
string jsonString = jsonData.ToString();
List<orResult> result = JsonConvert.DeserializeObject<List<orResult>>(jsonString);
return result;

The JSON can be seen here.可以在此处查看 JSON。 Json杰森

When I run the app, I get the following error.当我运行该应用程序时,出现以下错误。

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code Newtonsoft.Json.dll 中发生了“Newtonsoft.Json.JsonReaderException”类型的异常,但未在用户代码中处理

Additional information: Unexpected character encountered while parsing value: M. Path '', line 0, position 0.附加信息:解析值时遇到意外字符:M。路径 '',第 0 行,位置 0。

When I hover look at the path variable, it points to the right spot.当我悬停查看路径变量时,它指向正确的位置。 If I copy and paste the path variable in to my browser, I see the JSON.如果我将路径变量复制并粘贴到浏览器中,我会看到 JSON。 The error is something with Parsing the data or something... I have no idea.错误是解析数据或其他东西......我不知道。 Need help!需要帮忙!

I've looked at other solutions on Stack and none of them resolved my problem.我查看了 Stack 上的其他解决方案,但没有一个解决了我的问题。

JObject.Parse() expects the actual JSON content (string), not a path. JObject.Parse()需要实际的 JSON 内容(字符串),而不是路径。

Your JSON is really an array so you could use JArray.Parse() instead.您的 JSON 实际上是一个数组,因此您可以改用JArray.Parse() Also, converting JSON string to JObject and then back ToString() is really not adding any "value" here.此外,将 JSON 字符串转换为JObject然后再转换回ToString()实际上并没有在此处添加任何“值”。


This should do it.这应该这样做。

string json = File.ReadAllText(HttpContext.Current.Server.MapPath("~/testing/testData.json"));
var result = JsonConvert.DeserializeObject<List<orResult>>(json);

You are attempting to deserialize the translated path ("c:\\whatever\\...\\testing\\testData.json") thatMapPath returns, ie not the content of the file.您正在尝试反序列化MapPath返回的已翻译路径(“c:\\whatever\\...\\testing\\testData.json”),即不是文件的内容

Try something like this instead:试试这样的:

JObject jsonData = JObject.Parse(File.ReadAllText(path));

... but then again, there is no need to use Parse() as you don't need the JObject . ...但话又说回来,没有必要使用Parse()因为你不需要JObject Just deserialize directly to a List<orResult> :只需直接反序列化为List<orResult>

var result = JsonConvert.DeserializeObject<List<orResult>>(File.ReadAllText(path));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JsonReaderException - 解析值时遇到意外字符 - JsonReaderException - Unexpected character encountered while parsing value JsonReaderException:在 C# 中解析时遇到意外字符 - JsonReaderException: Unexpected character encountered while parsing in C# json.net,JsonReaderException:解析值后,遇到意外字符 - json.net, JsonReaderException: After parsing a value an unexpected character was encountered Newtonsoft.Json.JsonReaderException:&#39;解析值时遇到意外字符:[。 - Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [. Newtonsoft.Json.JsonReaderException:'解析值时遇到意外字符: - Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符 - Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value Newtonsoft.Json.JsonReaderException:在 JsonConvert.SerializeObject 之后解析 JsonConvert.DeserializeObject 时遇到意外字符 - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing on JsonConvert.DeserializeObject after JsonConvert.SerializeObject 错误 Newtonsoft.Json.JsonReaderException:&#39;解析值时遇到意外字符:S. 路径&#39;&#39;,第 0 行,位置 0。 - Error Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: S. Path '', line 0, position 0.' 未处理的异常。 Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:[。 路径“数据”,第 1 行,position 19 - Unhandled exception. Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: [. Path 'data', line 1, position 19 Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0. 仅在特定版本上 - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: S. Path '', line 0, position 0. Only on certain build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM