我是这个json和node.js的新手。现在我有json文件,其中有一些不必要的空格。如何删除?
{
"Product_Name": "Logitech M235 Wireless Mouse (Red)",
"Brand": "Logitech",
"Color": "\n \n Red\n \n ",
"Image": "http://ecx.images-amazon.com/images/I/31UXrp3AcLL._SY300_QL70_.jpg",
"Price": " 699.00",
"Rating": "4.4 out of 5 stars"
}
颜色值包含一些空格,我不想要我只需要的空格' Red'。
这里我写的是json文件
var json = {Product_Name : "", Brand : "", Color : "", Image : "", Price : "", Rating : ""};
var P_name = $('#title').children().text();
var brand = $('#brand').text();
var color = $('.a-row').find('span.selection').text();
var price = $('#price').find('span.a-size-medium').text();
var rating = $('#averageCustomerReviews').find('span.a-icon-alt').text();
var image = $('.imgTagWrapper').children().attr('src');
json.Product_Name = P_name;
json.Brand = brand;
json.Color = color;
json.Price = price;
json.Rating = rating;
json.Image = image;
fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})
答案 0 :(得分:0)
只需用颜色变量替换字符串中的空格和换行符,就像这样
color.trim()