【问题标题】:Generating GeoJson, how to use NewLine?生成GeoJson,如何使用NewLine?
【发布时间】:2019-04-30 17:05:58
【问题描述】:

我有几个函数可以使用 CSV Excel 表并将数据转换为 GeoJsonP,以用于 Google 地图项目。在每个条目之后,我想使用一个 NewLine 命令,这样每个条目都将在自己的行上,而不是整个文件是一个非常长的行。

The code

The error message

我已经搜索并发现使用 \n 会导致错误,因此使用 \n 提到了多个答案,但这也不起作用。是覆盖 JSON.stringify 函数的唯一解决方案吗?

【问题讨论】:

  • 您应该在问题中包含您的代码,而不是作为图像。

标签: javascript json google-maps jsonp geojson


【解决方案1】:

使用模板文字是编写 JSON 的一种非常糟糕的方式。你应该先在JS中创建结构然后stringify它。

您可以使用 stringify 的 space parameter 返回一个格式化为您要求的字符串。

const arr = [
  { name: 'Bob', age: 12 },
  { name: 'Sam', age: 21 },
  { name: 'Karen', age: 45 }
];

// Without
console.log(JSON.stringify(arr));
 
// With
console.log(JSON.stringify(arr, null, 2));

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 2018-12-03
    • 2012-11-25
    • 2019-08-11
    • 2014-02-04
    • 2015-08-30
    • 2013-06-29
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多