简体   繁体   English

通过“ \\” Node.js分割字符串

[英]Split string by “\” Node.js

Hy everyone, I'm having some issues with split a string because when I log her value seems ok but in the final result appears "\\" like, 大家好,我在分割字符串时遇到了一些问题,因为当我登录时,她的值似乎还可以,但是最终结果显示为“ \\”,例如

"map":"{\"isRoot\": true, \"visible\": true}" instead of have "map":"{"isRoot": true, "visible": true}"

So I have this code 所以我有这段代码

if(mapData!=undefined){
    var map = mapData.map;
    console.log("sendDeviceInfo getServicesMapInfoById map  >>>>>>>>>>>>>>>>>>>>> ", map);
    sendData.map=map;
    createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'sendDeviceInfo','sendData.map 1', sendData.map);
}

And my logs: 和我的日志:

sendDeviceInfo getServicesMapInfoById map  >>>>>>>>>>>>>>>>>>>>>  {"isRoot": true, "visible": true}
4|wscontro | [2017-05-30 12:36:03.010] - debug: /opt/wscontroller/wscontroller-service/scripts/gps GpsController 58a8c61b-f11d-4874-91df-3a5205e4145f sendDeviceInfo sendData.map 1 "{\"isRoot\": true, \"visible\": true}"

Why is this happening? 为什么会这样呢?

--- solution ---解决方案

if(mapData!=undefined){
    var map = mapData.map;
    var aux = map.split('\\').join('');
    var jsonObject = JSON.parse(aux);
    sendData.map = jsonObject;  
}

You can replace them like this 您可以像这样替换它们

yourJsonString = yourJsonString.split('\\').join('');
var jsonObject = JSON.parse(yourJsonString);

That's nothing to worry about. 没什么好担心的。 That "\\"s don't actually exist in the string. 字符串中实际上不存在“ \\”。

When you use JSON.stringify in node.js, the result always has "\\"s to escape special characters, and double quotes are special characters. 在node.js中使用JSON.stringify时,结果始终带有“ \\”以转义特殊字符,而双引号是特殊字符。 That character is the unix escape character. 该字符是unix转义字符。 That's why it appears kind of everywhere. 这就是为什么它无处不在的原因。

尝试使用以下代码删除转义序列

mapData = mapData.replace(/\\"/g, '"');

Your Json string is using "-marks. Since Json is a string itself, we need a solution to tell the compiler which "-marks are marking the string, and which "-marks are part of the string itself. 您的Json字符串使用的是“-”标记。由于Json本身是字符串,因此我们需要一种解决方案来告诉编译器哪些“-”标记正在标记字符串,哪些“-”标记是字符串本身的一部分。

To do this it's very common among languages to use the -character, and by typing for instance \\", you're 'escaping' the "-mark. 为此,在语言中使用-字符是很常见的,并且通过键入例如\\“,可以“转义”-标记。

Imagine a string like this: He said to me: "Hi there". 想象一下这样的字符串:他对我说:“嗨,在那里。” Now if we want to make this into a string we could try "He said to me: "Hi there".", though the compiler will see 2 strings and a lost period, because it does not distinguish between the start and end of the string, and the "-marks that are part of the content itself. 现在,如果我们想将其转换为字符串,我们可以尝试“他对我说:“嗨,那里”。”,尽管编译器会看到2个字符串和一个丢失的时期,因为它不能区分字符串的开始和结尾。字符串,以及内容本身的“-”标记。

When you console.log() an actual string, it will not show the "-marks that show the start and end of the string, because it is not necessary. Because of that, there is no need to escape the "-marks in the string itself. 当您使用console.log()实际字符串时,它不会显示“-”标记来显示字符串的开头和结尾,因为这是不必要的。因此,无需在其中转义“-”标记。字符串本身。

Whatever the createLog() function does, it apparently needs to note the string down as it being an actual string, and therefor it needs to escape the "-marks with a '\\' sign. 无论createLog()函数做什么,它显然都需要将字符串记下来,因为它是实际字符串,因此它需要转义带有'\\'符号的“-”标记。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM