【问题标题】:Httpresponse HandlingHttp响应处理
【发布时间】:2019-09-05 06:47:08
【问题描述】:

我想使用 httpresponse 作为 json 对象,以便我使用其中的值,例如 internalid、itemid 等 我得到了 httpresponse 并能够制作它的流,但这对我不起作用,因为它会返回字符串

1- 作为响应,我得到 JSON 字符串 2-我尝试使用 JSONConvert 方法进行解析 3- 我得到了带有 StreamReader 的 httpresponse 并且能够在控制台中显示响应字符串。

[WebResponse response = request.GetResponse();            
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Content length is {0}", httpResponse.ContentLength);
Console.WriteLine("Content type is {0}",httpResponse.ContentType);         
var response1 = (HttpWebResponse)request.GetResponse();
var rawJson = new StreamReader(response1.GetResponseStream()).ReadToEnd();
Console.WriteLine(rawJson);][1]

上面提到的这段代码返回“字符串”,我想像“对象”一样使用它

【问题讨论】:

    标签: c# restlet httpwebresponse


    【解决方案1】:

    您可以使用 Newtonsoft 解析为动态:Deserialize json object into dynamic object using Json.net

    或者,反序列化为强类型对象。要生成对象模型,您有多种选择:

    https://visualstudiomagazine.com/Blogs/Tool-Tracker/2018/02/paste-json-and-xml-as-class.aspx

    https://app.quicktype.io/#l=cs&r=json2csharp

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2012-01-03
      • 2015-05-19
      • 2014-07-28
      • 2018-07-15
      • 2012-09-12
      相关资源
      最近更新 更多