最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Remove the tab space or line breaks in a javascript string - Stack Overflow

programmeradmin0浏览0评论

I have a JSON file and i am getting the json content which may contain new lines in it. Now i am facing a problem. I am getting the below error :

Unexpected token ↵ in JSON at position 9

Note: the error message says specificaly this symbol: '↵'. What does this mean?

My JSON:

{"T":". 
^00:00:43^2008-09-11 12:00:00.0"}

But if i remove the new line (not sure if its a new line or a tab space no matter what i am facing this issue even if its a tab space or new line. It works fine with normal space.). Below is the modified JSON and it looks good.

Modified JSON:

 {"T":".^00:00:43^2008-09-11 12:00:00.0"}

I have tried to follow similar threads like here and here and some similar threads. but nothing works even after applying those. Any help is appreciated here please.

This is one the solution i have tried:

var jsonString = JSON.stringify(data).replace(/(\r\n|\n|\r)/gm,"");

Is there anyway to detect these linebreaks and tab spaces and remove them?

I have a JSON file and i am getting the json content which may contain new lines in it. Now i am facing a problem. I am getting the below error :

Unexpected token ↵ in JSON at position 9

Note: the error message says specificaly this symbol: '↵'. What does this mean?

My JSON:

{"T":". 
^00:00:43^2008-09-11 12:00:00.0"}

But if i remove the new line (not sure if its a new line or a tab space no matter what i am facing this issue even if its a tab space or new line. It works fine with normal space.). Below is the modified JSON and it looks good.

Modified JSON:

 {"T":".^00:00:43^2008-09-11 12:00:00.0"}

I have tried to follow similar threads like here and here and some similar threads. but nothing works even after applying those. Any help is appreciated here please.

This is one the solution i have tried:

var jsonString = JSON.stringify(data).replace(/(\r\n|\n|\r)/gm,"");

Is there anyway to detect these linebreaks and tab spaces and remove them?

Share Improve this question edited Jan 4, 2018 at 2:59 Barmar 783k56 gold badges547 silver badges660 bronze badges asked Jan 4, 2018 at 2:34 Stack AccStack Acc 792 gold badges2 silver badges9 bronze badges 10
  • About the , it's a symbol used to visualize a newline. – Luka Čelebić Commented Jan 4, 2018 at 2:37
  • @PredatorIWD so in this case its teh new line which is causing the problem in the jsonstring? – Stack Acc Commented Jan 4, 2018 at 2:38
  • What does yourString.charCodeAt(9) show? – Barmar Commented Jan 4, 2018 at 2:38
  • I believe in the above JSon 8th char is space and 9th is a new line here..I tried to replace new lines with normal space but thats not working either. @Barmar. So just trying to take suggestions here – Stack Acc Commented Jan 4, 2018 at 2:39
  • 1 Post your code, otherwise we're just guessing at what you might be doing wrong. – Barmar Commented Jan 4, 2018 at 2:42
 |  Show 5 more ments

3 Answers 3

Reset to default 4

You shouldn't be calling JSON.stringify(). That will convert the newlines in the string to literal \n, which won't be matched by the escape sequences in the regexp. Just do the replacement directly on the bad data.

var data = `{"T":". 
^00:00:43^2008-09-11 12:00:00.0"}`;
var jsonString = data.replace(/(\r\n|\n|\r)/g,"");
var object = JSON.parse(jsonString);
console.log(object);

This worked for me:

var jsonString = data.replace(/[\n\r\t]/g,"");

That is not a valid JSON value. If you refer to https://www.json/, newline, carriage returns and etc will need to be included in the value itself.

There is no way to modify because the parser already throws the error.

The only solution I can think of is for the author of the JSON file to amend their value like your Modified JSON or if they want to preserve that newline,

{"T":".\n^00:00:43^2008-09-11 12:00:00.0"}
发布评论

评论列表(0)

  1. 暂无评论