2016-09-07 53 views
0

我正在使用Newtonsoft.dll創建一個Settings.json文件。我有一個代表設置的類,我在加載應用程序時保存和反序列化時序列化。除了一件事以外,一切工作都很快速。VB.net JSON DeserializeObject錯誤BC30203

settings類包含一個名爲SpeechSynthesizer的.NET對象。當設置被反序列化時,我在SpeechSynthesizerVoice子對象上得到BC30203: Identifier expected。這會導致反序列化返回默認的`語音'(Anna)設置而不是保存的設置對象(序列化設置對象確實按照它應該工作的方式)在此子對象正確反序列化之前的對象設置。

的JSON:

{  ...... 
    "Speech": { 
    "State": 0, 
    "Rate": -3, 
    "Volume": 67, 
    "Voice": { 
     "Gender": 1, 
     "Age": 30, 
     "Name": "IVONA 2 Ruben", 
     "Culture": "nl-NL", 
     "Id": "IVONA 2 Voice Ruben22", 
     "Description": "IVONA 2 Ruben - Dutch male voice [22kHz]", 
     "SupportedAudioFormats": [], 
     "AdditionalInfo": { 
     "Language": "413", 
     "Name": "IVONA 2 Ruben", 
     "Age": "Adult", 
     "Gender": "Male", 
     "Vendor": "IVONA Software Sp. z o. o." 
     } 
    } 
    } 

反序列化代碼:

Return JsonConvert.DeserializeObject(Of Settings)(File.ReadAllText(settingsfile))

序列化代碼:

Using _file As StreamWriter = New StreamWriter(SettingFilePath) 
     Dim serializer As New JsonSerializer() 
     serializer.Formatting = Formatting.Indented 
     serializer.Serialize(_file, Me) 
    End Using 

設置代碼:

''' <summary> 
''' Get or Set the properties that is used for the SpeechSynthesizer 
''' </summary> 
''' <returns></returns> 
Public Property Speech As SpeechSynthesizer 

Image of the error

+0

千萬不要使用這個,但是['Voice'](https://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.voice(v = vs.110).aspx)屬性是隻讀的,所以你可能只需要在你的設置中保存語音名稱,並使用具有名稱的'SpeechSynthesizer.SelectVoice',而不是嘗試序列化'SpeechSynthesizer'實例。 – Mark

+0

馬克,Thx爲您的答覆。我從來沒有發現過它是一個'只讀'對象。我現在將'VoiceInfo'保存爲一個獨立的設置,在啓動時通過'.SelectVoice'加載它。謝謝! :-) – Gforse

回答

0

回答我的問題是,馬克指出,對象是ReadOnly這意味着它在應用程序啓動不能DeserializeDeserializing

工作以防萬一是通過添加額外的設置我打電話SelectedVoice作爲一個字符串。啓動後,我使用SpeechSynthesizer.SelectVoice(_Settings.SelectedVoice)加載用戶設置的語音。