【发布时间】:2014-06-09 03:56:30
【问题描述】:
目前,我有一些问题。我在 Json.NET 中使用 C#。问题是我总是得到:
{"解析值时遇到意外字符:e.Path '', line 0, position 0."}
所以我使用 Json.NET 的方式如下。我有一个应该保存的类。该类如下所示:
public class stats
{
public string time { get; set; }
public string value { get; set; }
}
public class ViewerStatsFormat
{
public List<stats> viewerstats { get; set; }
public String version { get; set; }
public ViewerStatsFormat(bool chk)
{
this.viewerstats = new List<stats>();
}
}
这个类的一个对象将被填充和保存:
File.WriteAllText(tmpfile, JsonConvert.SerializeObject(current), Encoding.UTF8);
保存部分工作正常,文件存在并已填充。之后,该文件将被读回类中:
try
{
ViewerStatsFormat current = JsonConvert.DeserializeObject<ViewerStatsFormat>(tmpfile);
//otherstuff
}
catch(Exception ex)
{
//error loging stuff
}
现在在 current= 行出现了异常:
{"解析值时遇到意外字符:e.Path '', line 0, position 0."}
我不知道为什么会这样。 JSON文件如下 -> Click me I am the JSON link
有人有什么想法吗?
【问题讨论】:
-
无论数据如何,您是否总是面临这个问题?有一次我遇到这样的问题,原因是服务发送了无效的 UTF-8 字符。
-
@ankhuri 只是忘记读取文件...我的错
-
假设这不是问题,还有什么? recv Exception-> {"topic":"robot1/Log","Msg":"Pilot Running"} 解析值时遇到意外字符:o。路径'',第 0 行,第 0 位置。
-
如何将其作为异常处理?
-
我找到了上面的答案。 stackoverflow.com/a/55951414/495157
标签: c# json visual-studio-2013 json.net