js判断数据类型 && 判断是否为标准json格式
/**
* 1. js判断对象的好方法
* 2. 判断是否为json格式化数据
*
* Author: shujun
* Date: 2020-8-09
*/ import {print} from './utils'; /**
* 1. js判断对象的好方法; 基本类型就用typeof
* // 根据typeof判断对象也不太准确
表达式 返回值
typeof undefined 'undefined'
typeof null 'object' 需要注意的
typeof true 'boolean'
typeof 123 'number'
typeof "abc" 'string'
typeof function() {} 'function'
typeof {} 'object' 需要区别的
typeof [] 'object' 需要区别的
tpyeof "" 'string' 需要注意的 * @param {*} a
*/
export function tellType(a){
print(a);
print("typeof: ", typeof a);
// typeof写的不好呀,object 和 array都返回 ‘object’, 还需要再判断
if ("object" === typeof a) {
if (Array.isArray(a)) {
print("is array")
} else {
print("is object");
}
}
return typeof a;
} // tellType("shujun");
// print(tellType(123) === "number");
// tellType({"a": 2});
// tellType([1,2, 3]); // 2. 判断是否为json格式化数据
function testJson(str) {
try {
let jsonObj = JSON.parse(str);
print("jsonObj tpyeof: ", typeof jsonObj);
if ("string" === typeof str && "object" === typeof jsonObj) {
return true;
}
} catch (error) {
print(error);
}
return false; } print(testJson('{"a":1, "b":2}'));
print(testJson('[1, 2, 3]'));
print(testJson('[1, 2, 3],2'));
print(testJson(123));
print(testJson("123"));
print(testJson("null")); //这里还是有bug,厉害,厉害
print(testJson(null));
js判断数据类型 && 判断是否为标准json格式的更多相关文章
- 标准JSON格式定义与解析注意点
标准JSON格式定义与解析注意点 在JS.IOS.Android中都内置了JSON的序列化.反序列化SDK.JEE中也可以使用第三方的JSON解析库,如GSON.虽然在JSON格式被定义出来的时候并没 ...
- js基本数据类型+判断数据类型方法
摘要:不管是什么类型的,Object.prototype.toString.call();都可以判断出其具体的类型,简单基本类型(String.Number.Boolean.Null.Undefine ...
- js判断字符串是否为正确的JSON格式及JSON格式化的实现
判断是否是正确的JSON格式 function isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); i ...
- Js中数据类型判断的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- JS的数据类型判断函数、数组对象结构处理、日期转换函数,浏览器类型判断函数合集
工具地址:https://github.com/BothEyes1993/bes-jstools bes-jstools 100多个基础常用JS函数和各种数据转换处理集合大全,此工具包是在 outil ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- JS实现的一个query字符串转Json格式数据的方法
输入字符串的格式是 a=1&b=2&c=3 $.par2Json = function (string, overwrite) { var obj = {}, pairs = stri ...
- js 表单内容使用ajax以json格式混合提交
脚本代码 function submitForm(){ var post_data = $("#form1").getdict(); var data_dict = { ...
- JS的jsoneditor,用来操作Json格式的界面;json-editor用来根据json数据生成界面
1.jsoneditor https://github.com/josdejong/jsoneditor https://jsoneditoronline.org/ 效果如下: 2.json-edit ...
- 关于非标准json格式转变为json对象
eval('(' + tempData + ')') 只需要这一句
随机推荐
- 【Linux命令】在Linux服务器上与windows通过SCP命令互传文件时出现的问题排查过程
1,在linux 执行 scp 1.txt adminitrator@10.10.10.10:/d:/后,报连接超时 原因:windows不支持ssh,可以安装支持SSH服务的工具,如:winsshd ...
- MAC怎么快速截图
1.截取全屏 按住[command][shift][3]这三个键即可截取全屏. 编辑 2.截取某区域 按住[command][shift][4]. 编辑 3.截图完成 在电脑桌面能找到截图 ...
- 去除Bam文件中的PCR 重复
1.使用samtools 去除重复 samtools sort -n -@ 20 file.bam |samtools fixmate -m -@20 - - |samtools sort -@ 20 ...
- pychars的使用
1|0安装 pyecharts 兼容 Python2 和 Python3.目前版本为 0.1.2 pip install pyecharts 2|0入门 首先开始来绘制你的第一个图表 from pye ...
- 分享: 第二性 合卷本 竖本.PDF
书本详情 第二性台版 作者: 西蒙.德.波娃(Simone de Beauvoir)出版社: 貓頭鷹原作名: Le Deuxième Sexe译者: 邱瑞鑾出版年: 2013-10页数: 1136装帧 ...
- 002. html篇之《表格》
html篇之<表格> 1. 结构 <table> <!-- 表格标签 --> <caption>标题,自动居中对齐</caption> &l ...
- G1 垃圾回收详解
引用: https://www.cnblogs.com/ciel717/p/16190562.html
- Kafka -- 基本操作
kafka 环境的安装 docker run -d --name zookeeper_zh --restart always --network kafka-net -e ALLOW_ANONYMOU ...
- 掌控安全学院SQL注入靶场-布尔盲注(一)
靶场地址:http://inject2.lab.aqlab.cn/Pass-10/index.php?id=1 判断注入点: http://inject2.lab.aqlab.cn/Pass-10/i ...
- webpack和source map
当 webpack 打包源代码时,可能会很难追踪到 error(错误) 和 warning(警告) 在源代码中的原始位置. 如果打包后代码有一处错误,特别是使用的vue.react这些前端框架.打包后 ...