c# - 解析值后遇到意外字符 : 6. Path '[0]

标签 c# json wpf json.net

我正在使用 Newtonsoft 库 (Json.Net) 从文件中解析一个 JSON 对象。我在 jsonlint.com 验证了 JSON , 它是有效的。

但是当我说:

using (StreamReader sr = new StreamReader(path))
{
    json = await sr.ReadToEndAsync();
}
ContactsCollection = JsonConvert.DeserializeObject<List<Contact>>(json); //error

我得到一个错误:

After parsing a value an unexpected character was encountered: 6. Path '[0]

所以我在 json = await sr.ReadToEndAsync(); 处设置了一个断点,显示的 JSON 值是:

"\0{\0\"\0F\0i\0r\0s\0t\0N\0a\0m\0e\0\"\0:\0\"\0N\0i\0k\0h\0\"\0,\0\"\0L\0a\0s\0t\0N\0a\0m\0e\0\"\0:\0\"\0A\0N\0S\0\"\0,\0\"\0D\0a\0t\0e\0O\0f\0B\0i\0r\0t\0h\0\"\0:\0\"\01\02\0/\07\0/\01\09\08\09\0\01\02\0:\00\00\0:\00\00\0\0A\0M\0\"\0,\0\"\0W\0e\0i\0g\0h\0t\0\"\0:\01\06\08\0.\00\0,\0\"\0H\0e\0i\0g\0h\0t\0\"\0:\01\06\08\0.\00\0,\0\"\0P\0h\0o\0n\0e\0\"\0:\0\"\0(\08\00\05\0)\0\02\05\01\0-\01\00\01\05\0\"\0}\0]\0"

这是我的实际 JSON:

[{
  "FirstName":"Nikh",
  "LastName":"ANS",
  "DateOfBirth":"12/7/1989 12:00:00 AM",
  "Weight":168.0,
  "Height":168.0,
  "Phone":"(805) 251-1015"
}]

这是我的联系人类:

public class Contact : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            NotifyPropertyChanged("FirstName");
        }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            _lastName = value;
            NotifyPropertyChanged("LastName");
        }
    }

    private string _dateOfBirth;
    public string DateOfBirth
    {
        get { return _dateOfBirth; }
        set
        {
            _dateOfBirth = value;
            NotifyPropertyChanged("DateOfBirth");
        }
    }

    private double _weight;
    public double Weight
    {
        get { return _weight; }
        set
        {
            _weight = value;
            NotifyPropertyChanged("Weight");
        }
    }

    private double _height;
    public double Height
    {
        get { return _height; }
        set
        {
            _height = value;
            NotifyPropertyChanged("Height");
        }
    }

    private string _phone;
    public string Phone
    {
        get { return _phone; }
        set
        {
            _phone = value;
            NotifyPropertyChanged("Phone");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

有谁知道可能出了什么问题吗?

最佳答案

这看起来像是一个编码问题。我敢打赌您的文件是以 UTF-16 编码保存的,但您正在以 UTF-8 格式读取它。 (UTF-8 是 StreamReader 的默认编码。)这可以解释为什么在读取的 JSON 值中间有各种 \0 字符,以及为什么 Json .Net 无法解析它。尝试在初始化 StreamReader 时指定编码:

    using (StreamReader sr = new StreamReader(path, Encoding.Unicode, true))
    {
        ...
    }

或者,确保您的 JSON 文件以 UTF-8 编码保存。

关于c# - 解析值后遇到意外字符 : 6. Path '[0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900333/

相关文章:

c# - 如何通过单击 xamarin 表单中的按钮打开网页

javascript - 将 Firestore 文档作为普通 Javascript 对象获取?

wpf - 如何使 StackPanel 或 DockPanel 拉伸(stretch)以适应 WPF 中的容器?

c# - nopCommerce框架如何使用MySQL数据库?

c# - WPF Richtextbox FontFace/FontSize

c# - 将 KeyVaultClient 与 MSAL token "Unauthorized"结合使用

JSON.NET 使用 linq 选择数组中的项目

ios - 获取 Laravel JSON feed 中帖子的页码

c# - WPF 异步等待任务锁定 UI 线程并行运行任务

c# - wpf 按字母顺序排序组合框项目