JSON之间的空格.stringify输出

Space in between JSON.stringify output

本文关键字:stringify 输出 空格 之间 JSON      更新时间:2023-09-26

我使用Jquery从页面获取所有产品名称,然后将其放在数组中。我正在使用这个代码

 <script type="text/javascript">
 jQuery(document).ready(function($) {
    var products = $(".product-name").map(function() {
    return { name: $(this).text() }
 }) .get();
 console.log(JSON.stringify(products));
 });
 </script>

输出格式为

[{"名称":"样本产品名称"},{"名称":"样本产品名称2"}]

我想要实现的是在"之后这两个对象之间有一个空格,所以输出将看起来像这样

[{"名称":"样本产品名称"},{"名称":"样本产品名称2"}]

建议吗?我挣扎了几个小时,却没有成功。

这是文件http://jsfiddle.net/2MeMY/1/

这可能不是你想要的,但如果你只是想让它看起来更好,我建议:

console.log(JSON.stringify(products, null, 2));

等于

[
  {
    "name": "Sample Product Name"
  },
  {
    "name": "Sample Product Name 2"
  }
]

在控制台上。如果你真的只想在逗号前加一个空格,你可以这样做:

console.log(JSON.stringify(products).split('},{').join('}, {'));
http://jsfiddle.net/2MeMY/3/

您也可以这样做,使用replace

console.log(JSON.stringify(products).replace(/},{/g,'}, {')); 
// /},{/g means all occurance of },{

如果您希望json输出为:

  1. 单行的
  2. 在道具名和道具值之间(以及项目之间)有空格
  3. 每个逗号和下一个prop-name/item之间有空格

你可以这样写:

function Stringify_WithSpaces(obj) {
	let result = JSON.stringify(obj, null, 1); // stringify, with line-breaks and indents
	result = result.replace(/^ +/gm, " "); // remove all but the first space for each line
	result = result.replace(/'n/g, ""); // remove line-breaks
	result = result.replace(/{ /g, "{").replace(/ }/g, "}"); // remove spaces between object-braces and first/last props
	result = result.replace(/'[ /g, "[").replace(/ ']/g, "]"); // remove spaces between array-brackets and first/last items
	return result;
}
let obj = [{name: "Sample Product Name"}, {name: "Sample Product Name 2"}];
console.log("Stringified with spaces: " + Stringify_WithSpaces(obj));

下面是单行表达式形式的函数:

JSON.stringify(obj, null, 1).replace(/^ +/gm, " ").replace(/'n/g, "").replace(/{ /g, "{").replace(/ }/g, "}").replace(/'[ /g, "[").replace(/ ']/g, "]")

扩展Typescript版本


这里是一个更详细的版本(在Typescript中),带有选项:

export class ToJSON_WithSpaces_Options {
    insideObjectBraces = false;
    insideArrayBrackets = false;
    betweenPropsOrItems = true;
    betweenPropNameAndValue = true;
}
export function ToJSON_WithSpaces(obj, options?: Partial<ToJSON_WithSpaces_Options>) {
    options = Object.assign({}, new ToJSON_WithSpaces_Options(), options);
    let result = JSON.stringify(obj, null, 1); // stringify, with line-breaks and indents
    result = result.replace(/^ +/gm, " "); // remove all but the first space for each line
    result = result.replace(/'n/g, ""); // remove line-breaks
    if (!options.insideObjectBraces) result = result.replace(/{ /g, "{").replace(/ }/g, "}");
    if (!options.insideArrayBrackets) result = result.replace(/'[ /g, "[").replace(/ ']/g, "]");
    if (!options.betweenPropsOrItems) result = result.replace(/, /g, ",");
    if (!options.betweenPropNameAndValue) result = result.replace(/": /g, `":`);
    return result;
}

理想情况下,这类函数将在之前应用正则表达式来删除换行符(这样它就可以保证它不会修改用户提供的字符串中的文本),但我将把它留给其他人做,因为上面的用例已经足够了(我认为大多数其他的)。