简体   繁体   English

替换为\\ n <br> 在JSON字符串中

[英]Replace \n with <br> in JSON string

I have a function to grab text data as JSON, which then is stringified. 我有一个函数来抓取文本数据作为JSON,然后进行字符串化。 I'm trying to display this data in a modal in my web application, however the string appears in the HTML like this: 我正在尝试在我的Web应用程序中以模式显示此数据,但字符串在HTML中显示如下:

{"body":"---\\nbl_playout_user: BLplayout\\n\\n# Playout config values\\nbl_playout_image_drive_mount: '\\\\Paparazzi\\Images'\\nbl_playout_image_path: 'I:'\\nbl_playout_machine_role: Primary\\nbl_playout_network_name: ABC1\\nbl_playout_stand_alone_mode: 0\\nbl_playout_run_mode: HD\\nbl_playout_format: AJA\\nbl_playout_resolution: 720P\\nbl_playout_logo_override: ABC\\nbl_playout_default_header: BottomLine\\nbl_playout_sponsor_on: 15000\\nbl_playout_sponsor_off: 60000\\nbl_playout_media_ball_on: 15000\\nbl_playout_media_ball_off: 15000\\nbl_playout_page_engine_working_directory: '{{ bl_templates_root_dir }}\\SC14_Templates'\\nbl_playout_schema_file_path: '{{ bl_templates_root_dir }}\\SC14_Templates\\BLMaster.xsd'\\nbl_playout_default_swatch_path: '{{ bl_templates_root_dir }}\\SC14_Templates\\003_BtmLn_deliverable_ele\\still_ele\\RDBL_GENERIC_SWATCH.png'\\nbl_playout_default_logo_path: '{{ bl_templates_root_dir }}\\SC14_Templates\\003_BtmLn_deliverable_ele\\still_ele\\default_team_logo.png'\\n"} {“body”:“--- \\ nbl_playout_user:BLplayout \\ n \\ n#播放配置值\\ nbl_playout_image_drive_mount:'\\\\ Paparazzi \\ Images'\\ nbl_playout_image_path:'我:'\\ nbl_playout_machine_role:Primary \\ nbl_playout_network_name:ABC1 \\ nbl_playout_stand_alone_mode:0 \\ nbl_playout_run_mode:HD \\ nbl_playout_format:AJA \\ nbl_playout_resolution:720P \\ nbl_playout_logo_override:ABC \\ nbl_playout_default_header:底线\\ nbl_playout_sponsor_on:15000 \\ nbl_playout_sponsor_off:60000 \\ nbl_playout_media_ball_on:15000 \\ nbl_playout_media_ball_off:15000 \\ nbl_playout_page_engine_working_directory: '{{bl_templates_root_dir}} \\ SC14_Templates' \\ nbl_playout_schema_file_path : '{{bl_templates_root_dir}} \\ SC14_Templates \\ BLMaster.xsd' \\ nbl_playout_default_swatch_path: '{{bl_templates_root_dir}} \\ SC14_Templates \\ 003_BtmLn_deliverable_ele \\ still_ele \\ RDBL_GENERIC_SWATCH.png' \\ nbl_playout_default_logo_path:“{{bl_templates_root_dir}} \\ SC14_Templates \\ 003_BtmLn_deliverable_ele \\ still_ele \\ default_team_logo.png'\\ n“}

I would like the instances of \\n to be replaced with a <br> so it shows up properly in the HTML, however my function doesn't seem to be doing the trick. 我希望将\\n的实例替换为<br>以便它在HTML中正确显示,但是我的功能似乎没有做到这一点。 Any ideas as to why? 任何想法为什么? I'm very stumped. 我很难过。

var stringData = JSON.stringify(data);
stringData = stringData.replace(new RegExp('\r?\n','g'), '<br />');
return stringData;

Try this. 尝试这个。

stringData = stringData.replace(new RegExp("\\\\n", "g"), "<br />");

or this 或这个

stringData = stringData.replace(/\\n/g, "<br />");

based on comments and building on other answer.. 基于评论和建立其他答案..

var json = JSON.parse(stringData);
json.body = body = json.body.replace(/\n/g, "<br />");

// and then if you needed it in a string you can do this again
stringData = JSON.stringify(json);

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

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