有志者事竟成。

js判断字符串数据类型

mounted() {
  this.isJSON('{"key":1232,"a":2},{"key":1232,"a":3}')
}, methods: {   isJSON(str) {   if (typeof str == "string") {   try {   var jsonObj = JSON.parse(str); if (typeof jsonObj == "object") {   if(Array.isArray(jsonObj)){ console.log('Array数据')//'[{"key":1232,"a":2},{"key":1232,"a":3}]' return 'Array'; }else{ console.log('json数据')//'{"key":1232,"a":2}' return 'json'; } } else if (typeof jsonObj == "number"){  console.log('number数据')//123 return 'number'; }else{ return typeof jsonObj; } } catch (e) { console.log("error" + str + "!!!" + e); if(str.indexOf("},") != -1){ let list = [] str.split('},').forEach(item=>{   if(item.indexOf("}") != -1){   list.push(JSON.parse(item))   }else{   list.push(JSON.parse(item + "}"))   }       }) console.log(list);//'{"key":1232,"a":2},{"key":1232,"a":3}' 拼成数据数组格式 } return 'string'; } }else{ console.log(typeof str) return typeof str; } }, }

 

posted on 2021-07-07 16:05  阿长*长  阅读(543)  评论(0编辑  收藏  举报

导航