Search code examples
c#asp.netjsonasp.net-mvcwebclient

WebClient.DownloadData() returns Json with html tags like <pre> from WebApi


I'm trying to read from our production api which always outputs the json surrounded by <pre> tags
The problem I am facing that when i use WebClient.DownloadData() it don't scrape the json only but also the <pre> tags which is unwanted
I managed to come up with an api that outputs the json in <pre> tags when i try to read from it actually scrape only the json Here is the working api https://pokeapi.co/api/v2/pokemon/pikachu/
I tried to copy the formatting in the production api but no use

String Json = System.Text.Encoding.UTF8.GetString((byte[])(new System.Net.WebClient().DownloadData("https://pokeapi.co/api/v2/pokemon/pikachu/")));
Console.WriteLine(Json);
Json = System.Text.Encoding.UTF8.GetString((byte[])(new System.Net.WebClient().DownloadData("remote-api-url")));
Console.WriteLine(Json);

Why the <pre> tags does appear in the first print only and the sources of the two documents has those <pre> tags

Update:

It turned out that i use ContentResult to return put my json along with <pre> tags on the page so when i use this approach i get to output json with using the Formmatting.Indented .. Here is the code

return Content("<pre style =\"word-wrap: break-word; white-space: pre-wrap;\">" +new Response() { Status = "Success", Payload = null }.ToJsonString(Formatting.Indented)+ "</pre>");

thanks to rene's remark about the page source, I knew the following: with using ContentResult the source of the page is generated as a full html document unlike JsonResult which will output the source containing the json only and it will generate the <pre> tags automatically here is the code

return Json(new Response() { Status = "Success", Payload = null },JsonBehaviour.AllowGet);

What if i wanted to have my json indented ? then the ContentResult approach should be used in the API along with David's Answer to get rid of the <pre> tags in the agent.
<<given that the server has properly escaped the inner xml content if there is any>>


Solution

  • Just treat the response as XML and extract the value of the root node.

    eg

                var xml = @"
    <pre style =""word - wrap: break-word; white - space:
    pre - wrap; "">{""Status"":""Success"",""Payload"":null}</pre>";
    
                var json = XDocument.Parse(xml).Root.Value;