10

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 using C# Winforms with preferably no outside APIs. I can provide additional code/fiddler results if you need them.

The fiddler and gibberish images:

GibberishJSON

The gibberish came from my attempts to read the stream in the code below:

Stream sw = requirejs.GetRequestStream(); 
sw.Write(logBytes, 0, logBytes.Length); 
sw.Close(); 
response = (HttpWebResponse)requirejs.GetResponse();
Stream stream = response.GetResponseStream(); 
StreamReader sr = new StreamReader(stream); 
MessageBox.Show(sr.ReadToEnd());
Chris Altig
  • 680
  • 3
  • 8
  • 22
  • "preferably no outside APIs." Use an outside API, like Newtonsoft.Json (also called JSON.NET). – Steve Wellens Dec 07 '13 at 03:43
  • Do you need it as a string for display, or do you need to utilize the data contained within (meaning you need it available as an array/list)? – brandonscript Dec 07 '13 at 03:46
  • @SteveWellens - OP wants to read response as string which indeed requires no external APIs. Parsing may need JSON.Net... Code looks reasonable. Obviously creation is missing and OP may need to read about [encodings](http://www.joelonsoftware.com/articles/Unicode.html) which is likley the reason of the output. – Alexei Levenkov Dec 07 '13 at 03:47
  • @AlexeiLevenkov - Thank you for reiterating the OP's desires. I think he will find that trying to create an object from a JSON string is a lot of work and using a 3rd party library is an acceptable way to go. I was nudging him in the direction he will likely go. – Steve Wellens Dec 07 '13 at 03:54
  • @r3mus I need it as a string so I can then send it as a header – Chris Altig Dec 07 '13 at 05:08

2 Answers2

18

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());
stames
  • 1,250
  • 1
  • 10
  • 16
  • Thanks! Does HTTPWebRequest in an HTTPS connection prevent it from being deserialized? Because the login is an HTTPS connection, and I know the JSON is valid, but when I attempt to deserialize it, it doesn't succeed, saying "Invalid JSON Primitive". Am I missing something here? – Chris Altig Dec 07 '13 at 05:32
  • I've never had an issue decoding responses from an HTTPS connection. What's the raw text you get back that the decode fails on? – stames Dec 10 '13 at 00:09
3

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);
brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • 1
    How to find System.Web.Helpers: http://stackoverflow.com/questions/8037895/where-can-i-find-system-web-helpers-system-web-webpages-and-system-web-razor – Steve Wellens Dec 07 '13 at 03:59