1

I tried using Uri.UnescapeDataString to unescape JavaScript encoded URL. Heres the sample URL:

https://drive.google.com/open?id\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\u0026usp\u003dsharing

When I tried using Uri.UnescapeDataString in C# Interactive window, it correctly unescape the URL.

Microsoft (R) Roslyn C# Compiler version 2.8.3.63029
Loading context from 'CSharpInteractive.rsp'.
Type "#help" for more information.
> Uri.UnescapeDataString("https://drive.google.com/open? 
id\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\u0026usp\u003dsharing)
"https://drive.google.com/open?id=1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr&usp=sharing"

But in real application, it just don't want to unescape. I tried from Immediate Window.

? uri
"https://drive.google.com/open?id\\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\\u0026usp\\u003dsharing"
Uri.UnescapeDataString(uri) 
"https://drive.google.com/open?id\\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\\u0026usp\\u003dsharing"

Solution

Below code is working for me using Newtonsoft.Json JObject.

 var json = "{\"su\": \"" + uri + "\"}";
 var ss = JObject.Parse(json);

 return ss["su"].Value<string>();
Fahmi Noor Fiqri
  • 441
  • 7
  • 15
  • notice the double slashes in the "real application"? – Garr Godfrey Sep 16 '18 at 00:59
  • basically, the C# parser converts the literal string to unicode when you use \u, but a string read externally will have an actually \ and letter u. – Garr Godfrey Sep 16 '18 at 01:01
  • possible dup. https://stackoverflow.com/questions/12676746/parse-json-string-in-c-sharp – Garr Godfrey Sep 16 '18 at 01:04
  • When I open up the `uri` in Text Visualizer, the URL is shown as `https://drive.google.com/open?id\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\u0026usp\u003dsharing`, so the double slash means unescape the slash (\\ -> \) – Fahmi Noor Fiqri Sep 16 '18 at 01:09
  • 1
    I know it is confusing, but C#, Java, Javascript, C, etc all use \ to escape special characters. When reading strings from other sources they look different than how you must type them in C#. UnescapeDataString will not do what you want it to do. – Garr Godfrey Sep 16 '18 at 01:22

1 Answers1

0

Notice the difference in these two strings:

"https://drive.google.com/open?id\\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\\u0026usp\\u003dsharing"
"https://drive.google.com/open?id\u003d1n1hiV2sDFVctI8Qc9Z3EWPvEBO6KstFr\u0026usp\u003dsharing"

The first is the string you printed out in the "real" application, and the second is what you typed into the command line interpreter.

The command line interpreter, like a compiler, it what is converting \uXXXX into unicode characters, not the call to UnescapeDataString. UnescapeDataString decodes url encoded strings (like %20 characters).

Your best bet is to use Json parsing of some kind. For something simple like this, System.Web.Script.Serialization.JavaScriptSerializer is adequate.

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23