小编典典

解析值时遇到意外字符

c#

目前,我有一些问题。我在Json.NET中使用C#。问题是我总是得到:

{“解析值时遇到意外字符:e。路径”,第0行,位置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。路径”,第0行,位置0。”}

我不知道为什么会这样。json文件如下->
单击我的JSON链接

有人有什么想法吗?


阅读 1456

收藏
2020-05-19

共1个答案

小编典典

可能您没有将JSON传递给DeserializeObject

它看起来像从File.WriteAllText(tmpfile,...该类型tmpfile就是string包含文件路径。JsonConvert.DeserializeObject采用JSON值,而不是文件路径-
因此尝试转换类似@"c:\temp\fooo"-显然不是JSON 失败。

2020-05-19