2017-02-24 51 views
1

我寫UWP應用程序,我使用HttpClient()與服務器通信UWP解析或反序列化的HttpClient響應

下面的代碼,我到目前爲止已經試過

public async void POSTreq() 
    { 
     Uri requestUri = new Uri("http://www.example.com"); 
     string myParameters = "_action=LOGIN&[email protected]&password=123456789"; 
     json = JsonConvert.SerializeObject(myParameters); 
     var objClint = new System.Net.Http.HttpClient(); 
     System.Net.Http.HttpResponseMessage respon = await objClint.PostAsync(requestUri, new StringContent(myParameters, Encoding.UTF8, "application/x-www-form-urlencoded")); 
     var responJsonText = await respon.Content.ReadAsStringAsync();    
     Debug.WriteLine(responJsonText); 
    } 

我不知道該怎麼迴應轉換數據字典或列表格式

{"redirecturl":"https:\/\/www.example.com","status":"success","timestamp":1487906895,"community":"","communitystr":null,"currentworkspace":"w","schemarevision":null,"persona":null,"username":"[email protected]","isadmin":"false","email1status":null} 

上面的字符串應該轉換爲

output['redirecturl']="https:/\/\www.example.com" 

output['status']="success" etc.. 
+0

作爲註釋只需記住來處理網絡異常 – Corcus

回答

3

我想你要找的東西是這樣的:

var responJsonText = await respon.Content.ReadAsStringAsync(); 
Dictionary<string, string> output= JsonConvert.DeserializeObject<Dictionary<string, string>>(responJsonText); 

您現在可以訪問屬性的值是這樣的:output['redirecturl']

希望它能幫助!

+0

謝謝兄弟..你救了我的一天 –

0

我看到你以前接受的答案,而是另一種方法是使用動態變量,結合JSON對象時非常有用。