2017-05-10 51 views
3

我解析一個JSON對象從文件使用Newtonsoft庫(Json.Net)。我在jsonlint.com驗證了JSON,並且它是有效的。解析值後遇到意外的字符:6.路徑'[0]

但是,當我說:

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

有誰知道什麼可能會錯了嗎?

+4

你可以包括你的「聯繫」類嗎? – degant

+0

請把你的樣品與更多的細節,如聯繫類 –

回答

3

這看起來像一個編碼問題。我打賭你的文件是以UTF-16編碼保存的,但你正在以UTF-8格式讀取它。 (UTF-8是StreamReader的默認編碼。)這可以解釋爲什麼在讀取JSON值的過程中會出現各種各樣的\0字符,以及Json.Net爲何無法解析它。嘗試指定編碼當你初始化StreamReader

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

此外,請確保您的JSON文件保存UTF-8編碼。

+0

謝謝布賴恩這是一個編碼問題 – nikhil

+0

沒問題;很高興我能幫上忙。 –

0

檢查流路徑的位置設置是否正確。也許增加行:path.Position = 0;之前致電sr.ReadToEndAsync();

相關問題