【发布时间】:2016-05-15 07:32:01
【问题描述】:
我是这个 json 和 node.js 的新手。现在我有 json 文件,其中有一些不必要的空格。怎么去掉?
.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"
}
颜色值包含一些空格,我不希望这些空格我只需要“红色”。
我正在写 json 文件
server.js
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');
})
【问题讨论】: