在分析值意外遇到性格 [英] Unexpected character encountered while parsing value

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

问题描述

目前我得到了一些问题。即时通讯使用C#与Json.NET。问题是,我总是得到

currently i got some issues. Im 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 they 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
}



来了异常:

Now on the current= line comes the exception:

{,而解析值意外的人物遇到的:亦即路径',0行,位置0}

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

我不知道为什么会这样来了。 JSON文件在以下 - > 点击我IM的JSON链接

I dont know why this comes. The json file is the following -> Click me im the JSON link

有没有人有什么想法?

推荐答案

也许你不通过JSON到 DeserializeObject

Possibly your are not passing JSON to DeserializeObject.

它看起来像从 File.WriteAllText(TMPFILE,... 该类型 TMPFILE 字符串包含文件路径。 JsonConvert.DeserializeObject 采用JSON价值,而不是文件路径 - 所以它没有试图像转换@C:\temp\fooo - 这显然不是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:\temp\fooo" - which is clearly not JSON.

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

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