Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove \n after JSON.stringfy?

Tags:

javascript

I parse the data from website and try to change to a json object.

Here is my function:

function outPutJSON() {

        for (var i = 0; i < movieTitle.length; i++) {

            var handleN = movieContent[i];
            console.log('===\n');
            console.log(handleN);

            data.movie.push({
                mpvieTitle: movieTitle[i],
                movieEnTitle: movieEnTitle[i],
                theDate: theDate[i],
                theLength: theLength[i],
                movieVersion: movieVersion[i],
                youtubeId: twoId[i],
                content: movieContent[i]
            });
        };

        return JSON.stringify(data);
    }

console.log will print movieContent[0] like:

enter image description here

but i return JSON.stringfy(data); it will become: enter image description here

There are so many /n i want to remove it.

I try to change return JSON.stringfy(data); to this:

var allMovieData = JSON.stringify(data);
allMovieData = allMovieData.replace(/\n/g, '');
return allMovieData;

It's not working the result is the same.

How to remove /n when i use JSON.stringfy() ?

Any help would be appreciated . Thanks in advance.

like image 364
Morton Avatar asked Sep 01 '17 07:09

Morton


People also ask

What is opposite of Stringify?

stringify() is the opposite of JSON. parse(), which converts JSON into Javascript objects. This article and Udacity's JSON.

Does JSON Stringify remove spaces?

Calling JSON. stringify(data) returns a JSON string of the given data. This JSON string doesn't include any spaces or line breaks by default.


1 Answers

In your data screenshots, you literally see "\n".

This probably means that the actual string doesn't contain a newline character (\n), but a escaped newline character (\\n).

A newline character would have been rendered as a linebreak. You wouldn't see the \n.

To remove those, use .replace(/\\n/g, '') instead of .replace(/\n/g, '')

like image 51
Cerbrus Avatar answered Oct 16 '22 19:10

Cerbrus