简体   繁体   English

如何忽略Javascript / C#中的Unicode转义序列?

[英]How can I ignore Unicode escape sequences in Javascript/C#?

I'm developing a web application using ASP.NET Core which displays routes on Google Maps using their API. 我正在使用ASP.NET Core开发一个Web应用程序,该应用程序使用其API在Google Maps上显示路线。 One of the ways it does this is with encoded polyline data, which it gets from our backend server. 它的一种方法是使用编码的折线数据,该数据从我们的后端服务器获取。 The problem is that when I pull through a value like this: 问题是当我通过这样的值时:

{konItyiI??mLxJaRhPwY}C{[cUc\ua@qYw`@uYe_@{VgZqTwTiHfE_N~`@oOvg@iVtg@qVld@{Vv`@oV|_@cDbXtDz\jDr\~Hj[|GfXjDr[pLtZdItb@pIha@`BpQOdH??

Then Javascript throws an Uncaught SyntaxError: Invalid Unicode escape sequence and the map never displays. 然后,Javascript引发Uncaught SyntaxError: Invalid Unicode escape sequence ,并且映射从不显示。 This is because there's a "\\u\u0026quot; in the polyline data. 这是因为折线数据中有一个“ \\ u”。 It works fine with other polylines that don't contain this data. 它可以与其他不包含此数据的折线一起使用。

Is there a way to ignore Unicode escape sequences using Javascript? 有没有办法使用Javascript忽略Unicode转义序列? Everything I've found so far only relates to making the strings HTML-safe, which isn't the object of the exercise here. 到目前为止,我发现的所有内容仅与使字符串成为HTML安全字符串有关,这不是本文的练习目的。

Update: 更新:

I wasn't able to use: var coords = "@Models.Polyline".replace("\\\\", ""); 我无法使用: var coords = "@Models.Polyline".replace("\\\\", ""); because the exception was raised when the string was declared. 因为在声明字符串时引发了异常。

However, I could use: var coords = "@Models.Polyline.Replace("\\\\", "")"; 但是,我可以使用: var coords = "@Models.Polyline.Replace("\\\\", "")"; using the C# Replace() method instead of the JavaScript replace() method. 使用C#Replace()方法而不是JavaScript replace()方法。

This means that the data won't cause an exception when JavaScript tries to parse it into a string literal because the offending backslashes have been removed before JS sees it. 这意味着当JavaScript尝试将数据解析为字符串文字时,数据不会导致异常,因为在JS看到之前,已经删除了令人反感的反斜杠。

This causes a problem, though. 但是,这会引起问题。 The polyline data which is left over is missing enough data from the original that the route displayed on the map is almost completely different, especially over small distances. 剩下的折线数据缺少原始数据,因此地图上显示的路线几乎完全不同,尤其是在小距离上。

try following: 尝试以下操作:

step 1: 第1步:

// put the polyline data into string variable before you use it //在使用折线数据之前将其放入字符串变量中

string bfesc = "{konItyiI??mLxJaRhPwY}C{[cUc\\ua@qYw @uYe_@{VgZqTwTiHfE_N~ @oOvg@iVtg@qVld@{Vv @oV|_@cDbXtDz\\jDr\\~Hj[|GfXjDr[pLtZdItb@pIha@ BpQOdH??" 串bfesc =“{konItyiI ?? mLxJaRhPwY}℃{[CUC \\ UA @ QYW @uYe_@{VgZqTwTiHfE_N~ @ oOvg @ iVtg @ QVLD @ {VV @oV|_@cDbXtDz\\jDr\\~Hj[|GfXjDr[pLtZdItb@pIha@ BpQOdH ??”

Step 2: // remove \\u\u003c/i> 步骤2://删除\\ u

nestr = bfesc.replace("\\\\u\u0026quot;, ""); nestr = bfesc.replace(“ \\\\ u”,“”);

Now use nestr 现在使用Nestr

Hope it helps!! 希望能帮助到你!! cheers :) 欢呼:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript呈现unicode转义序列? - How to render unicode escape sequences using javascript? 如何在反应 javascript 中将 unicode 转义序列转换为 unicode 字符 - How to convert unicode escape sequences to unicode characters in react javascript 对于Unicode转义序列,忽略jslint``不必要的转义''错误 - ignore jslint 'unnecessary escapement' error for unicode escape sequences 在javascript或C#/ .NET中转义unicode(%u2014)? - Escape unicode (%u2014) in javascript or in C#/.NET? 如何使用 PHP 将 UTF-8 编码为 Unicode 转义序列,如“\Á”? - How to encode UTF-8 to Unicode escape sequences like "\u00C1" using PHP? json_encode Unicode转义序列无法在javascript中显示 - json_encode unicode escape sequences cannot display in javascript javascript标识符名称中的unicode转义序列有什么意义? - What is the point of unicode escape sequences in identifier names in JavaScript? 如何在python中转义UNICODE字符串(到javascript转义) - How to escape UNICODE string in python (to javascript escape) 如何使用jQuery或纯JavaScript检索Unicode转义序列? - How do I retrieve Unicode escape sequence with jQuery or plain JavaScript? 如何在JavaScript中检测和删除无效的unicode序列 - How to detect and remove invalid unicode sequences in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM