toJSON
方法可以将 FuzzyFAIRLY 对象转化为 JSON 格式的字符串。
fuzzify.toJSON()
一个 JSON 格式的字符串。
// 创建 FuzzyFAIRLY 对象
const temperature = new FuzzyFAIRLY({
universe: [0, 50],
terms: [
{ name: "cold", points: [0, 20], type: "triangle" },
{ name: "medium", points: [15, 35, 45], type: "trapezoid" },
{ name: "hot", points: [30, 50], type: "triangle" }
]
});
// 转换为 JSON 字符串
const jsonString = temperature.toJSON();
console.log(jsonString);
//输出: '{"universe":[0,50],"terms":[{"name":"cold","points":[0,20],"type":"triangle"},{"name":"medium","points":[15,35,45],"type":"trapezoid"},{"name":"hot","points":[30,50],"type":"triangle"}]}'
toJSON
方法返回的是一个 JSON 字符串,而不是一个 JSON 对象。JSON.parse()
方法转为 JSON 对象。