Search code examples
c#jsonencodingspecial-charactersdecoding

HtmlDecode - you’ll


public static UserItem DownloadJSONString(string urlJson)
{
    using (WebClient wc = new WebClient())
    {
        var json = wc.DownloadString(urlJson);
        UserItem userItems = JsonConvert.DeserializeObject<RootObject>(json);

        return userItems;
    }            
}

I'm working on Json file to deserialzing C# poco class something like this:

var root = JsonConvert.DeserializeObject<RootObject>(json);

i have noticed that its translating from you’ll to you’ll i'm not sure where does it coming from and I have looked at the json file in the browser and it is rendering as you’ll NOT you’ll

i tried HttpUtility.HtmlDecode but does not decode.

PS: i am not sure if this help or not but i'm using Newtonsoft.Json for deserializing


Solution

  • This is probably not an issue specific to the JSON parser.

    More likely, the issue is with how you are acquiring json. It appears to be an encoding issue -- perhaps you are reading from a file which is using a Windows-1252 or ISO-8859-1 encoding, but the reader is treating it as UTF-8. Or vice versa.

    WebClient.DownloadString uses a supplied Encoding to do this conversion. You need to set it explicitly:

    public static UserItem DownloadJSONString(string urlJson)
    {
        using (WebClient wc = new WebClient())
        {
            wc.Encoding = Encoding.UTF8;
    
            var json = wc.DownloadString(urlJson);
            UserItem userItems = JsonConvert.DeserializeObject<RootObject>(json);
    
            return userItems;
        }            
    }