javascript - 用双引号替换 JSON 对象值单引号
问题描述
我的 JSON 响应值有单引号,但我想要双引号。我已经尝试过了JSON.stringfy()
,JSON.parse()
它们都不起作用。
回复:
[
{
title: 'Car',
price: 2323,
}
]
预期反应:
[
{
title: "Car",
price: 2323,
}
]
基本上,我想在 shopify graphql 查询中使用该响应。
mutation {
productCreate(input: {
id:"gid://shopify/Product/4725894742116"
title: "This is a car",
variants:[{
title:"car",
price: 12
}]
}) {
product {
id
}
}
}
解决方案
我没有看到,任何使用问题JSON.stringify
都可以直接获取字符串并在查询中使用它,或者如果你需要一个 javascript 对象,你可以解析它。
const unwantedResponse = [{
title: 'Car',
price: 2323,
}]
const wantedResponse = JSON.stringify(unwantedResponse);
const parsedResponse = JSON.parse(wantedResponse)
console.log(wantedResponse);
console.log(parsedResponse);