NodeJS – 在Windows中解析嵌套JSON字符串时出现问题

huangapple go评论91阅读模式
英文:

NodeJS - Problem when parsing JSON string inside another JSON on Windows

问题

我的Node.js应用程序接收一个包含JSON字符串值的参数。例如:

node index.js --json-items='[{"a": 10, "b": 20}]'

index.js中:

const { jsonItems } = argv
const items = JSON.parse(jsonItems)
// 可行...

所以我通过package.json脚本(JSON转义)自动化了它:

{
  "scripts": {
    "dev": "node index.js --json-items='[{\"a\": 10, \"b\": 20}]'"
   }
}

在我的MacOS上仍然可以工作,因为参数被正确转义:

const { jsonItems } = argv
// (字符串) '[{"a": 10, "b": 20}]'

但是在Windows上不行,因为双引号被移除了:

const { jsonItems } = argv
// (字符串) '[{a: 10, b: 20}]'
// JSON.parse() -> 抛出异常:Unexpected token...

如何解决这个问题?

或者只是在JSON.parse()之前如何转换:

'[{a: 10, b: 20}]' -> '[{"a": 10, "b": 20}]'

我正在使用yargs包来获取参数。

英文:

My nodejs application receives an argument that contains a JSON string as value. For example:

node index.js --json-items='[{"a": 10, "b": 20}]'

And inside index.js:

const { jsonItems } = argv
const items = JSON.parse(jsonItems)
// Works...

So I made it automatic via package.json scripts (JSON scaped):

{
  "scripts": {
    "dev": "node index.js --json-items='[{\"a\": 10, \"b\": 20}]'"
   }
}

On my MacOS it still working because the arg is scaped correctly:

const { jsonItems } = argv
// (string) '[{"a": 10, "b": 20}]'

But on a Windows it doesn't because double quotes are removed:

const { jsonItems } = argv
// (string) '[{a: 10, b: 20}]'
// JSON.parse() -> Uncaught SyntaxError: Unexpected token...

How to resolve this?

Or just how to convert before JSON.parse():

'[{a: 10, b: 20}]' -> '[{"a": 10, "b": 20}]'

> I'm using yargs package to get the arguments.

答案1

得分: 1

你可以使用这个正则表达式在每个对象键周围添加双引号,所以你可以在JSON.parse()之前使用这个:

let validJSON = '[{a: 10, b: 20}]'.replace(/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/g, '$1"$3":')

console.log(validJSON);
console.log(JSON.parse(validJSON));
英文:

You can use this regex to wrap double quotes around each Object Key, so you could just us this before the JSON.parse()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let validJSON = &#39;[{a: 10, b: 20}]&#39;.replace(/([{,])(\s*)([A-Za-z0-9_\-]+?)\s*:/g, &#39;$1&quot;$3&quot;:&#39;)

console.log(validJSON);
console.log(JSON.parse(validJSON));

<!-- end snippet -->

答案2

得分: 1

这似乎是与Windows、NPM和最终Node处理引号的方式有关的问题。

"dev": "node index.js --json-items=\"[{\\\"a\\\": 10, \\\"b\\\": 20}]\""

首先,您需要为JSON转义一次,然后需要再次为Windows/Node转义。此外,&#39; 似乎也不太有效,我建议使用 \&quot;

英文:

This seems to be an issue with how Windows and NPM and then finally Node handle the quotes.

&quot;dev&quot;: &quot;node index.js --json-items=\&quot;[{\\\&quot;a\\\&quot;: 10, \\\&quot;b\\\&quot;: 20}]\&quot;&quot;

You first need to escape it once for the json, and then you need to escape it again for windows/node. Also &#39; does not seem to work too good either so I recommend \&quot;.

huangapple
  • 本文由 发表于 2020年1月3日 22:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定