JSON Bool&Number无引号

1.注意

和后端一块儿定义API时须要注意JSON格式:number和bool是没有引号的。 (今天就由于后端给的API返回了一个字符串"true",致使前端犯了一个低级错误。 后端给的json是{ "feature-B": "true", "feature-A": "false"} json.parse()后获得一个字符串"true/false",经过!!"false" 强制转换获得的值仍为true,意义混乱致使错误)前端

let featureEnabled = !!this.store[toggleName];
复制代码

2.注意:

JSON中的数字类型和Bool类型 是不能放在双引号里面的,不然就成字符串类型了。json

let myObj = { name: "John", age: 31, city: "New York" ,flag:true};
let myJSON = JSON.stringify(myObj);
console.log(myJSON);

复制代码
// result.json :
{
  "name": "John",
  "age": 31,
  "city": "New York",
  "flag": true
}
复制代码
相关文章
相关标签/搜索