I have retrieved JSON data content of a web page and want to use it in app.
我检索了网页的JSON数据内容,并希望在应用程序中使用它。
One of the key's value contain data with "\n and commented out lines". I want to remove them and use only simple text information.
其中一个键的值包含带有“\ n和注释掉的行”的数据。我想删除它们并仅使用简单的文本信息。
I can't put my code because the "commented line" are not visible here.
我无法放入我的代码,因为这里看不到“注释行”。
Here is a demo:
这是一个演示:
{"content":"Hello \n 'CommentedLinesHere' \n how are you? " }
So, is there any way to get “Hello how are you?” here?
那么,有没有办法让“你好,你好吗?”在这里?
Any help would be appreciated.
任何帮助,将不胜感激。
3 个解决方案
#1
2
Use the global flag from regex to globally replace your char and then turn it back into a JSON object
使用regex中的全局标志来全局替换您的char,然后将其转换回JSON对象
var noNewLines = JSON.stringify(myJson);
noNewLines = noNewLines.replace(/\n/g, "");
var backToJson= JSON.parse(noNewLines );
#2
1
var objJsonData = {"content":"Hello \n 'CommentedLinesHere' \n how are you? "}
var ResultJson = objJsonData.content.replace(/\n/g, '').replace("CommentedLinesHere",'').replace("'",'').replace("'",'');
alert(ResultJson);
#3
1
You need to use a regex to replace all the \n
newline characters with a blank space. See the code below:
您需要使用正则表达式将所有\ n换行符替换为空格。请参阅以下代码:
var obj = {"content": "Hello \n 'CommentedLinesHere' \n how are you? "}
var str = obj.content.replace(/\n(.|\n|\r)+\n/g, "");
alert(str);
#1
2
Use the global flag from regex to globally replace your char and then turn it back into a JSON object
使用regex中的全局标志来全局替换您的char,然后将其转换回JSON对象
var noNewLines = JSON.stringify(myJson);
noNewLines = noNewLines.replace(/\n/g, "");
var backToJson= JSON.parse(noNewLines );
#2
1
var objJsonData = {"content":"Hello \n 'CommentedLinesHere' \n how are you? "}
var ResultJson = objJsonData.content.replace(/\n/g, '').replace("CommentedLinesHere",'').replace("'",'').replace("'",'');
alert(ResultJson);
#3
1
You need to use a regex to replace all the \n
newline characters with a blank space. See the code below:
您需要使用正则表达式将所有\ n换行符替换为空格。请参阅以下代码:
var obj = {"content": "Hello \n 'CommentedLinesHere' \n how are you? "}
var str = obj.content.replace(/\n(.|\n|\r)+\n/g, "");
alert(str);