wpf 解析值后遇到意外字符:6. 路径 '[0]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43900333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
After parsing a value an unexpected character was encountered: 6. Path '[0]
提问by nikhil
I am parsing a JSON object from a file using Newtonsoft library (Json.Net). I validated the JSON at jsonlint.com, and it's valid.
我正在使用 Newtonsoft 库 (Json.Net) 从文件中解析 JSON 对象。我在jsonlint.com验证了 JSON ,它是有效的。
But when I say:
但是当我说:
using (StreamReader sr = new StreamReader(path))
{
json = await sr.ReadToEndAsync();
}
ContactsCollection = JsonConvert.DeserializeObject<List<Contact>>(json); //error
I get an error:
我收到一个错误:
After parsing a value an unexpected character was encountered: 6. Path '[0]
解析值后遇到意外字符:6. 路径 '[0]
So I put a breakpoint at json = await sr.ReadToEndAsync();and the JSON value shown is:
所以我放了一个断点,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"
"\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"
Here's my actual JSON:
这是我的实际 JSON:
[{
"FirstName":"Nikh",
"LastName":"ANS",
"DateOfBirth":"12/7/1989 12:00:00 AM",
"Weight":168.0,
"Height":168.0,
"Phone":"(805) 251-1015"
}]
Here's my Contact class:
这是我的 Contact 类:
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));
}
}
}
Does anyone know what might be going wrong?
有谁知道可能会出什么问题?
采纳答案by Brian Rogers
This looks like an encoding issue. I'm betting your file is saved in UTF-16 encoding, but you are reading it as UTF-8. (UTF-8 is the default encoding for StreamReader.) This would explain why there are all kinds of \0characters in the middle of your read JSON value, and why Json.Net is having trouble parsing it. Try specifying the encoding when you initialize the StreamReader:
这看起来像是编码问题。我打赌你的文件是以 UTF-16 编码保存的,但你正在阅读它作为 UTF-8。(UTF-8 是 的默认编码StreamReader。)这将解释为什么\0在您读取的 JSON 值中间有各种字符,以及为什么 Json.Net 解析它时遇到问题。初始化时尝试指定编码StreamReader:
using (StreamReader sr = new StreamReader(path, Encoding.Unicode, true))
{
...
}
Alternatively, make sure your JSON file is saved with UTF-8 encoding.
或者,确保您的 JSON 文件使用 UTF-8 编码保存。
回答by Anthony Gingrich
Check that the stream pathhas its position set appropriately. Perhaps add the line: path.Position = 0;before the call to sr.ReadToEndAsync();
检查流路径的位置设置是否正确。也许添加行:path.Position = 0;在调用之前sr.ReadToEndAsync();

