2012-07-10 140 views
1

我想解析從文件流中獲取的json文件。以下是我的json數據從json文件中檢索字典值

{ 
    "appname":"sine", 
    "taborder": [ 
      "some", 
      "thing", 
      "is", 
      "went", 
      "wrong" ] 
} 

我正在將數據存儲在字符串中並嘗試反序列化數據。我想,以顯示警告框的按鍵以下列方式

string jsonString = contents;//"{'Name':'Bill', 'Ag:53}"; 
you deserializedUser = ReadToObject(jsonString); 

var str = deserializedUser.mainDict.Keys.ToArray(); 
MessageBox.Show(str.ToString()); 

但我正在逐漸鍵值爲「空」,如何獲得在適當的鍵值,請幫助我.....

回答

1

我這樣做是希望這有助於

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
     { 
    JavaScriptRequest obj= DeserializeJavaScriptRequest(typeof(JavaScriptRequest), Resource1.test) as JavaScriptRequest; 
    MessageBox.Show(obj.appname +" | " + obj.taborder[0]); 
} 

public object DeserializeJavaScriptRequest(Type typedeserialize, string eValue) 
     { 

      Type t = typedeserialize; 
     // Get constructor info. 
     ConstructorInfo[] ci = t.GetConstructors();   
     object reflectOb = ci[0].Invoke(null); 
     MemoryStream confirm_ms = new MemoryStream(Encoding.UTF8.GetBytes(eValue)); 
     DataContractJsonSerializer confirm_ser = new DataContractJsonSerializer(typedeserialize); 
     reflectOb = confirm_ser.ReadObject(confirm_ms); 
     confirm_ms.Close(); 

     return reflectOb; 
    } 

而且我做了一個類烏爾答覆

public class JavaScriptRequest 
    { 
     public string appname { get; set; } 
     public string[] taborder { get; set; } 
    } 
+0

謝謝,(我上無法理解)什麼是Resource1.test? – Ram 2012-07-10 07:41:39

+0

@Ram對不起,它是json字符串 – CognitiveDesire 2012-07-10 09:45:47

+0

感謝您的答覆。但它引發此異常:參數空異常被處理。值不能爲空。 參數名稱:s – Ram 2012-07-11 04:58:41