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

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

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

保存部分工作正常,文件存在并已填充.之后,将使用以下命令将该文件读回到类中:

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

Now on the current= line comes the exception:

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

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

Does anyone have any ideas?

推荐答案

Possibly you are not passing JSON to DeserializeObject.

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.

Json相关问答推荐

根据条件向数组添加元素

手动解开没有可编码的SON- Swift

PostgreSQL 12.17从JSON数组提取元素

Vega-Lite:文本笔画在外部

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

在Ansible中从json中提取特定数据

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

使用 jolt 变换展平嵌套 JSON - 非常复杂

使用 Redis 作为键值存储

使用 jq 和脚本 bash 映射两个 json

小写嵌套特定键的震动转换

Shell脚本空格转义

使用 jq 和 awk 拆分大型 JSON 文件

使用 SwiftUI 在 API 调用中解码嵌套 JSON 响应时遇到问题

如何从 HttpClient 解析 JSON 字符串?

jQuery AJAX 和 JSON 格式

从 JSON 创建 Hashtable

如何判断 JSON 响应元素是否为数组?

Python 到 JSON 序列化在十进制上失败

JSON - 是否有任何 XML CDATA 类似功能?