开发者

Properly decode a text that contains strings like \u003c or \u00252 in c#

开发者 https://www.devze.com 2023-03-08 14:14 出处:网络
I have a JSon response that contains lots of \\u003c or \\u00252 or other similar strings inside. I need a开发者_开发问答 proper function in order to decode these strings into proper characters.There

I have a JSon response that contains lots of \u003c or \u00252 or other similar strings inside. I need a开发者_开发问答 proper function in order to decode these strings into proper characters.


There are various posts about how to deserialize JSON strings. Here shows a nice generic method for deserializing. The code below is taken from there.

public static T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    obj = (T)serializer.ReadObject(ms); // <== Your missing line
    return obj;
} 
}

Having re-read your post if you are just looking for a way to convert the string to ASCII then check out this post. ORiginal Creadit to @Adam Sills for this code

static string DecodeEncodedNonAsciiCharacters( string value ) {
    return Regex.Replace(
        value,
        @"\\u(?<Value>[a-zA-Z0-9]{4})",
        m => {
             return ((char) int.Parse( m.Groups["Value"].Value, NumberStyles.HexNumber )).ToString();
        } );
}


Note I'm assuming you just have the data part of the string, not an entire JSON fragment - i.e.

string s = @"blah \u003c blah \u00252 blah";

If the above assumption is wrong and you have a full JSON fragment, just use JavaScriptSerializer to get an object from the data.

Annoyingly, HttpUtility has encode but not decode.

You could spoof the string into a full JSON object, though - this seems a bit overkill:

class Dummy
{
    public string foo { get; set; }
}
static void Main(string[] args)
{
    string s = @"blah \u003c blah \u00252 blah";
    string json = @"{""foo"":""" + s + @"""}";
    string unencoded = new JavaScriptSerializer().Deserialize<Dummy>(json).foo;
}


I'm not sure but I think you can construct a char directly with the unicode character code:

char c='\003C'; // c|60 '<'
0

精彩评论

暂无评论...
验证码 换一张
取 消