解析值时遇到意外的字符

zAf*_*fLu 95 c# json json.net visual-studio-2013

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

{"解析值时遇到意外的字符: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>();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

 File.WriteAllText(tmpfile, JsonConvert.SerializeObject(current), Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)

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

    try 
{ 

    ViewerStatsFormat current = JsonConvert.DeserializeObject<ViewerStatsFormat>(tmpfile);
    //otherstuff        

}
catch(Exception ex)
{
    //error loging stuff
}
Run Code Online (Sandbox Code Playgroud)

现在在current =行上出现异常:

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

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

有没有人有任何想法?

Ale*_*kov 125

可能你没有传递JSON DeserializeObject.

它看起来像从File.WriteAllText(tmpfile,...该类型tmpfile就是string包含文件路径.JsonConvert.DeserializeObject获取JSON值,而不是文件路径 - 因此它无法尝试转换类似的东西@"c:\temp\fooo"- 显然不是JSON.


Edu*_*ais 67

我用这些在线工具解决了这个问题:

  1. 检查Json结构是否正常:http://jsonlint.com/
  2. 从我的Json结构生成我的Object类:http://json2csharp.com/

简单的代码:

RootObject rootObj= JsonConvert.DeserializeObject<RootObject>(File.ReadAllText(pathFile));
Run Code Online (Sandbox Code Playgroud)

  • ***How-to*** `检查Json 结构` 在C# 中是否正常? (3认同)

Ale*_*kin 19

就我而言,包含 JSON 字符串的文件具有BOM。一旦我删除了 BOM,问题就解决了。

在此处输入图片说明

  • 如果您有幸成为编写 JSON 的人,您可以防止 BOM 被这样编写:“using (var streamWriter = new StreamWriter(someStream, new UTF8Encoding(false))) ...” (2认同)

Bra*_*ick 13

我在Xamarin.Android解决方案中遇到了同样的错误.

我验证了我的JSON是正确的,并注意到只有在我将应用程序作为Release版本运行时才出现错误.

原来,Linker正在从Newtonsoft.JSON中删除一个库,导致JSON被错误地解析.

我通过将Newtonsoft.Json添加到Android Build Configuration中的Ignore程序集设置来修复错误(下面的屏幕截图)

JSON解析代码

static readonly JsonSerializer _serializer = new JsonSerializer();
static readonly HttpClient _client = new HttpClient();

static async Task<T> GetDataObjectFromAPI<T>(string apiUrl)
{
    using (var stream = await _client.GetStreamAsync(apiUrl).ConfigureAwait(false))
    using (var reader = new StreamReader(stream))
    using (var json = new JsonTextReader(reader))
    {
        if (json == null)
            return default(T);

        return _serializer.Deserialize<T>(json);
    }
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio Mac截图

在此输入图像描述

Visual Studio截图

在此输入图像描述


Ale*_*xei 10

我也遇到过这个错误对Web API(.NET 3.0的核心)的行动,是有约束力的string,而不是一个object或一个JObject。JSON 是正确的,但活页夹试图从 JSON 结构中获取字符串并失败。

所以,而不是:

[HttpPost("[action]")]
public object Search([FromBody] string data)
Run Code Online (Sandbox Code Playgroud)

我不得不使用更具体的:

[HttpPost("[action]")]
public object Search([FromBody] JObject data)
Run Code Online (Sandbox Code Playgroud)


小智 6

此问题与 JSON 文件中的字节顺序标记有关。JSON 文件在保存时不会被编码为 UTF8 编码数据。使用File.ReadAllText(pathFile)修复这个问题。

当我们对字节数据进行操作并将其转换为字符串然后传递给 JsonConvert.DeserializeObject 时,我们可以使用 UTF32 编码来获取字符串。

byte[] docBytes = File.ReadAllBytes(filePath);

string jsonString = Encoding.UTF32.GetString(docBytes);


小智 6

我在 ASP.NET core 中的 webapi 上遇到了同样的问题,就我而言,这是因为我的应用程序需要身份验证,然后它分配注释[AllowAnonymous]并且它起作用了。

\n\n
[AllowAnonymous]\npublic async Task <IList <IServic >> GetServices () {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\n}\n
Run Code Online (Sandbox Code Playgroud)\n