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 + ')') 只需要这一句
随机推荐
- 【Frida】打印方法的调用堆栈
function printstack() { console.log(Java.use("android.util.Log").getStackTraceString(Java. ...
- 实现两个APP之间的跳转传值
应用A 跳转到 应用B 1.在B中设置URL Schemes 加入一项item 并赋值,比如kiloMeter 并在B中实现 - (BOOL)application:(UIApplicatio ...
- iframe 父子页面调用vue函数,并解决跨域问题,宽度自适应
第一: 1. 父页面 html <iframe id="external-frame" name="external-frame" ref="i ...
- 【驱动 】frambuffer中显示屏参数的修改
1.在x210板子的kernel中,默认LCD显示屏是800*400的,修改在 kernel/arch/arm/mach-s5pv210/mach-x210.c 中 258行 #define S5PV ...
- java-Stream流的常见方法
一 常见创建流的三种方式 //list集合创建流方式 new ArrayList<>().stream(); new HashSet<>().stream(); //map集 ...
- NLP学习日记
数据读取 下载csv文件后使用excel进行转存,然后用pandas读取,再把读取后转为numpy,numpy的tensor里.-1代表数组的最大维度,将原始数据集的标签和特征集分开,便于下一步的处理 ...
- vscode中使用powershell显示分支名
https://blog.csdn.net/weixin_43932597/article/details/125000557 windows powershell(或windows terminal ...
- IDEA 项目运行内存设置
IDEA 运行内存设置 1.堆内存设置 2.help ---- edit custom VM options 参考摘录:https://blog.csdn.net/wentaolv/article/d ...
- linux 软链接 硬链接 区别
来源 https://www.cnblogs.com/oceanftd/p/13475643.html 相关概念: 链接:简单说,链接就是一种文件共享的方式,是POSIX中的概念,主流文件系统都支持 ...
- 解决habbybase 操作hbase报错TTransportException(type=4,message=’TSocket read 0 bytes)
1.确认集群的协议是否一致,在连接的时候,改成一致的.例如集群配置的是TFramedTransport,TCompactProtocol 时 改成 connection = happybase.Con ...