uwp 解析或反序列化 httpclient 响应
uwp parse or deserialize httpclient response
我正在为 UWP 编写应用程序,我正在使用 HttpClient()
与服务器通信
下面的代码我已经尝试过
public async void POSTreq()
{
Uri requestUri = new Uri("http://www.example.com");
string myParameters = "_action=LOGIN&username=xyz@abc.com&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":"xyz@abc.com","isadmin":"false","email1status":null}
以上字符串应转换为
output['redirecturl']="https:/\/\www.example.com"
output['status']="success" etc..
我想你要找的是这个:
var responJsonText = await respon.Content.ReadAsStringAsync();
Dictionary<string, string> output= JsonConvert.DeserializeObject<Dictionary<string, string>>(responJsonText);
您现在可以像这样访问属性的值:output['redirecturl']
希望对您有所帮助!
我看到你之前的回答被接受了,但另一种方法是使用动态变量,这在绑定 json 对象时很有用。
我正在为 UWP 编写应用程序,我正在使用 HttpClient()
与服务器通信
下面的代码我已经尝试过
public async void POSTreq()
{
Uri requestUri = new Uri("http://www.example.com");
string myParameters = "_action=LOGIN&username=xyz@abc.com&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":"xyz@abc.com","isadmin":"false","email1status":null}
以上字符串应转换为
output['redirecturl']="https:/\/\www.example.com"
output['status']="success" etc..
我想你要找的是这个:
var responJsonText = await respon.Content.ReadAsStringAsync();
Dictionary<string, string> output= JsonConvert.DeserializeObject<Dictionary<string, string>>(responJsonText);
您现在可以像这样访问属性的值:output['redirecturl']
希望对您有所帮助!
我看到你之前的回答被接受了,但另一种方法是使用动态变量,这在绑定 json 对象时很有用。