2014-08-30 127 views
2

您好,我無法使用我自己的類結構將此代碼實現到Windows Phone的Visual Basic中。我希望能夠保存我的對象的實例並從文件中檢索它們。一旦我找出寫作,閱讀應該是相同的。我從這個教程此代碼:Serialize Tutorial如何使用JSON序列化器序列化和反序列化MyClass?

private const string JSONFILENAME = "data.json"; 

private async Task writeJsonAsync() 
{ 
    // Notice that the write is ALMOST identical ... except for the serializer. 

    var myCars = buildObjectGraph(); 

    var serializer = new DataContractJsonSerializer(typeof(List<Car>)); 
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
       JSONFILENAME, 
       CreationCollisionOption.ReplaceExisting)) 
    { 
    serializer.WriteObject(stream, myCars); 
    } 

    resultTextBlock.Text = "Write succeeded"; 
} 

我的階級結構是這樣

MyClass1 inherits MyClass2 
MyClass2 inherits MyClass3 
MyClass4 

這是多遠,我已經得到了

Dim test as MyClass4 = new MyClass4 
Dim serializer as New DataContractJsonSeializer(MyClass4) 
Dim stream as System.IO.Stream = Await ApplicationData.Current.localfolder.OpenStreamForWriteAsync(dir,CreationCollisionOption.ReplaceExisting) 

的問題是,我可以不初始化串行器,因爲MyClass4是一個Type,不能用作表達式。但該參數是要求System.Type我很困惑。請幫忙。

Dim serializer as New DataContractJsonSeializer(MyClass4) //gives me an error here 
+0

我不是一個VB的傢伙,但是這可能會解決你的問題 '昏暗的串行器=新DataContractJsonSerializer(的GetType(MyClass4 ))'。 – nkchandra 2014-09-02 19:26:59

+0

謝謝你的工作。 – Zocoloni 2016-11-16 18:11:50

+0

@ Zocoloni:考慮接受他的答案,因爲它的工作。 – mrsrinivas 2016-11-22 14:17:34

回答

0

我不是一個VB的傢伙,但是這可能會解決你的問題

Dim serializer = New DataContractJsonSerializer(GetType(MyClass4));