Deserialize Json encountered URL change

只谈情不闲聊 提交于 2019-12-11 13:19:23

问题


I have an Json string return by facebook api and I want to cast it to an object, I tried using both Newton Json and JavaScriptSerializer.

https://graph.facebook.com/v1.0/1111111111111/comments?limit=25&after=NTA\u00253D

After I cast it to a strongly typed object or a dynamic object, the url will be changed to

https://graph.facebook.com/v1.0/1111111111111/comments?limit=25&after=NTA%3D

What is the cause of this issue?

I have tried url encoding and decoding, but it didn't work.


回答1:


In JSON, any character can be represented by a unicode escape sequence, which is defined as \u followed by 4 hexadecimal digits (see JSON.org). When you deserialize the JSON, each escape sequence is replaced by the actual unicode character. You can see this for yourself if you run the following example program:

class Program
{
    static void Main(string[] args)
    {
        string json = @"{ ""Test"" : ""\u0048\u0065\u006c\u006c\u006f"" }";
        Foo foo = JsonConvert.DeserializeObject<Foo>(json);
        Console.WriteLine(foo.Test);
    }
}

class Foo
{
    public string Test { get; set; }
}

Output:

Hello

In your example URL, \u0025 represents the % character. So the two URLs are actually equivalent. There is no issue here.



来源:https://stackoverflow.com/questions/24755407/deserialize-json-encountered-url-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!