-1

I am getting data in json format from a url. Everything is working perfect, but one small issue is that in the data that I am getting there are some special characters, for example :

Get 50% off on Pizzas between 11am – 5pm.

– here means that it is '-', but how can I decode it in c# so that it takes it as '-'.

I have tried using Html.decode method, it is working fine with URLs but not with data.

I cannot replace – with '-' everywhere because this is not a single case, there are other similar characters too.

miradulo
  • 28,857
  • 6
  • 80
  • 93
Alok Gupta
  • 1,353
  • 1
  • 13
  • 29

3 Answers3

2

Works fine:

https://dotnetfiddle.net/H9rpLe

using System;

public class Program
{
    public static void Main()
    {
        string data = System.Net.WebUtility.HtmlDecode("Get 50% off on Pizzas between 11am – 5pm"); 

        Console.WriteLine(data);
    }
}

Output: Get 50% off on Pizzas between 11am – 5pm

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • You are correct it works, but just to let you know your fiddle uses `–`, not `&ndash` - it will work in both cases though :-) – RB. Feb 18 '16 at 09:19
  • 1
    dotnetfiddle is broken in this: https://dotnetfiddle.net/1TA9GU - my original code was `WriteLine("a – b");` – Wapac Feb 18 '16 at 09:21
  • @RB. Weird, I copied paste if from the fiddle, where it showed correctly (with amp)... – RvdK Feb 18 '16 at 09:42
  • Sounds like Wapac has found the culprit - interesting :) – RB. Feb 18 '16 at 09:46
  • Thank you all for this information, The issue is solved. Thanks a lot. – Alok Gupta Feb 18 '16 at 17:48
1

I think this is a duplicate of this question.

You can use HttpUtility.HtmlDecode

If you are using .NET 4.0+ you can also use WebUtility.HtmlDecode which does not require an extra assembly reference as it is available in the System.Net namespace.

Community
  • 1
  • 1
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
0

Try decoding it twice as it seems you have it encoded twice. First decoding will convert – to –, then second decoding will make it .

using System;
using System.Web;

public class Test
{
    public static void Main()
    {
        string s = "Get 50% off on Pizzas between 11am – 5pm.";
        Console.WriteLine(s);

        string d = HttpUtility.HtmlDecode(s);
        Console.WriteLine(d);

        string e = HttpUtility.HtmlDecode(d);
        Console.WriteLine(e);
    }
}
Wapac
  • 4,058
  • 2
  • 20
  • 33