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

javascript - How to remove r from json? - Stack Overflow

programmeradmin5浏览0评论

I run the following

let res = e.target.result.split('\n').map(line => line.split(','));
var myData = $("#dvCSV").text(JSON.stringify(res, null, '  '));
myData = myData.replace("\r", " ");
$("#dvCSV").append(myData);

But I still get \r in the output

[
  [
    "New York",
    "Employee",
    "20\r"
  ],
  [
    "Singapore",
    "Employee",
    "15"
  ]

I run the following

let res = e.target.result.split('\n').map(line => line.split(','));
var myData = $("#dvCSV").text(JSON.stringify(res, null, '  '));
myData = myData.replace("\r", " ");
$("#dvCSV").append(myData);

But I still get \r in the output

[
  [
    "New York",
    "Employee",
    "20\r"
  ],
  [
    "Singapore",
    "Employee",
    "15"
  ]
Share Improve this question edited Mar 31, 2018 at 14:05 rob.m asked Mar 31, 2018 at 13:52 rob.mrob.m 10.6k21 gold badges88 silver badges175 bronze badges 3
  • You're using both "\r" and "/r" in your question, "\r" has special meaning (escape for a return character) which one do you really mean? It appears you're trying to remove the "\r" strings from the JSON output but it's not clear from your question. – ellipse-of-uncertainty Commented Mar 31, 2018 at 14:03
  • split('\r\n') in first line – chuck911 Commented Mar 31, 2018 at 14:05
  • @hyphenthis yup, updated thanks. – rob.m Commented Mar 31, 2018 at 14:05
Add a ment  | 

3 Answers 3

Reset to default 4

You need to escape the backslash, and do the replace at the stringify, where it is a string

JSON.stringify(res, null, '  ').replace("\\r", " ");

Stack snippet sample

var res = [
  [
    "New York",
    "Employee",
    "20\r"
  ],
  [
    "Singapore",
    "Employee",
    "15"
  ]
];

console.log(JSON.stringify(res, null, '  ').replace("\\r", " ") ) 

replace method only removes first instance. in your case, only first instance of "\r"

Use JSON.parse and stringify together, like

JSON.parse(JSON.stringify(res));
发布评论

评论列表(0)

  1. 暂无评论