解析值时遇到意外字符 [英] Unexpected character encountered while parsing value

查看:48
本文介绍了解析值时遇到意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently, I have some issues. I'm using C# with Json.NET. The issue is that I always get:

{"解析值时遇到意外字符:e.路径 '',第 0 行,位置 0."}

{"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."}

所以我使用 Json.NET 的方式如下.我有一个应该保存的类.这个类看起来像这样:

So the way I'm using Json.NET is the following. I have a Class which should be saved. The class looks like this:

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>();
    }
}

这个类的一个对象将被填充并保存:

One object of this class will be filled and saved with:

 File.WriteAllText(tmpfile, JsonConvert.SerializeObject(current), Encoding.UTF8);

保存部分工作正常,文件存在并已填充.之后,文件将被读回类:

The saving part works fine and the file exists and is filled. After that the file will be read back into the class with:

try 
{ 
    ViewerStatsFormat current = JsonConvert.DeserializeObject<ViewerStatsFormat>(tmpfile);
    //otherstuff        
}
catch(Exception ex)
{
    //error loging stuff
}

现在在 current= 行出现异常:

Now on the current= line comes the exception:

{"解析值时遇到意外字符:e.路径 '',第 0 行,位置 0."}

{"Unexpected character encountered while parsing value: e. Path '', line 0, position 0."}

我不知道为什么会这样.JSON 文件如下 ->点击我我是 JSON 链接

I don't know why this comes. The JSON file is the following -> Click me I am the JSON link

有人有什么想法吗?

推荐答案

可能您没有将 JSON 传递给 DeserializeObject.

Possibly you are not passing JSON to DeserializeObject.

File.WriteAllText(tmpfile,... 看来,tmpfile 的类型是包含文件路径的 string.JsonConvert.DeserializeObject 采用 JSON 值,而不是文件路径 - 因此它无法尝试转换类似 @"c: empfooo" 的内容 - 这显然不是 JSON.

It looks like from File.WriteAllText(tmpfile,... that type of tmpfile is string that contain path to a file. JsonConvert.DeserializeObject takes JSON value, not file path - so it fails trying to convert something like @"c: empfooo" - which is clearly not JSON.

这篇关于解析值时遇到意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆