Getting JSON data from a response stream and reading it as a string?

后端 未结 2 1178
有刺的猬
有刺的猬 2021-01-02 14:26

I am trying to read a response from a server that I receive when I send a POST request. Viewing fiddler, it says it is a JSON response. How do I decode it to a normal string

相关标签:
2条回答
  • 2021-01-02 15:10

    As mentioned in the comments, Newtonsoft.Json is really a good library and worth using -- very lightweight.

    If you really want to only use Microsoft's .NET libraries, also consider System.Web.Script.Serialization.JavaScriptSerializer.

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var jsonObject = serializer.DeserializeObject(sr.ReadToEnd());
    
    0 讨论(0)
  • 2021-01-02 15:18

    Going to assume (you haven't clarified yet) that you need to actually decode the stream, since A) retrieving a remote stream of text is well documented, and B) you can't do anything much with a non-decoded JSON stream.

    Your best course of action is to implement System.Web.Helpers.Json:

    using System.Web.Helpers.Json
    ...
    var jsonObj = Json.Decode(jsonStream);
    
    0 讨论(0)
提交回复
热议问题