【问题标题】:uwp parse or deserialize httpclient responseuwp 解析或反序列化 httpclient 响应
【发布时间】:2017-02-24 03:36:55
【问题描述】:

我为 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..

【问题讨论】:

  • 作为评论,请记住处理网络异常

标签: c# json uwp httpclient httpresponse


【解决方案1】:

我认为您正在寻找的是:

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

您现在可以像这样访问属性的值:output['redirecturl']

希望对你有帮助!

【讨论】:

  • 谢谢兄弟..你拯救了我的一天
【解决方案2】:

我看到你之前的回答被接受了,但另一种方法是使用动态变量,这在绑定 json 对象时很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2020-10-19
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多