2012-07-10 108 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