gpt4 book ai didi

javascript - JSON.parse 不适用于将 JSON 字符串转换为 JSON 对象

转载 作者:行者123 更新时间:2023-11-30 08:26:02 24 4
gpt4 key购买 nike

需要帮助将以下 JSON 字符串转换为 JSON 对象。即使字符串 JSON 也是有效的 json(由 https://jsonlint.com/ 验证)。

JSON:

{
"condition": "AND",
"rules": [{
"id": "amount",
"operator": "greater_or_equal",
"value": "900"
},
{
"condition": "AND",
"rules": [{
"id": "vendorname",
"operator": "equal",
"value": "US BANK NATIONAL ASSOCIATION"
},

{
"id": "vendorname",
"operator": "equal",
"value": "HANSEN SOLUTIONS LLC"
}
]
}
]

}

最佳答案

您的 JSON 字符串是多行的。多行字符串应使用模板文字存储,否则使用字符串连接来表示您的字符串。

下面的例子使用了模板字面量。用于表示多行字符串。

var str = `{
"condition": "AND",
"rules": [{
"id": "amount",
"operator": "greater_or_equal",
"value": "900"
},
{
"condition": "AND",
"rules": [{
"id": "vendorname",
"operator": "equal",
"value": "US BANK NATIONAL ASSOCIATION"
},

{
"id": "vendorname",
"operator": "equal",
"value": "HANSEN SOLUTIONS LLC"
}
]
}
]
}`;

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

这是一个单行字符串。

var str = '{"condition":"AND","rules":[{"id":"amount","operator":"greater_or_equal","value":"900"},{"condition":"AND","rules":[{"id":"vendorname","operator":"equal","value":"US BANK NATIONAL ASSOCIATION"},{"id":"vendorname","operator":"equal","value":"HANSEN SOLUTIONS  LLC"}]}]}';

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

关于javascript - JSON.parse 不适用于将 JSON 字符串转换为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45519562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com