Load a JSON string in C #

I am trying to load a JSON string in my Windows Store application, which should look like this:

{ "status": "okay", "result": {"id":"1", "type":"monument", "description":"The Spire", "latitude":"53.34978", "longitude":"-6.260316", "private": "{\"tag\":\"david\"}"} } 

but I get server information. The output I get is as follows:

 Response: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { MS-Author-Via: DAV Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Date: Thu, 22 Nov 2012 15:13:53 GMT Server: Apache/2.2.22 Server: (Unix) Server: DAV/2 Server: PHP/5.3.15 Server: with Server: Suhosin-Patch Server: mod_ssl/2.2.22 Server: OpenSSL/0.9.8r X-Powered-By: PHP/5.3.15 Content-Length: 159 Content-Type: text/json } 

I look around and see that WebClient was used before Windows 8 and is now replaced by HttpClient. So instead of using DownloadString (), I used Content.ReadAsString (). Here's a bit of code that I still have:

 public async Task<string> GetjsonStream() { HttpClient client = new HttpClient(); string url = "http://(urlHere)"; HttpResponseMessage response = await client.GetAsync(url); Debug.WriteLine("Response: " + response); return await response.Content.ReadAsStringAsync(); } 

Does anyone know where I am going wrong? Thanks in advance!

+8
json string c #
source share
1 answer

You print the server response. The server response contains a StreamContent (see the documentation here ), but this StreamContent does not define ToString , so instead the class name is displayed instead of content.

ReadAsStringAsync (documentation here ) is the right way to get content sent by the server. Instead, you should print the return value of this call:

 public async Task<string> GetjsonStream() { HttpClient client = new HttpClient(); string url = "http://(urlHere)"; HttpResponseMessage response = await client.GetAsync(url); string content = await response.Content.ReadAsStringAsync(); Debug.WriteLine("Content: " + content); return content; } 
+14
source share

All Articles