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 + ')') 只需要这一句
随机推荐
- 【Java SpringBoot】RestTemplate使用postForObject发送请求,接收String类型的response时中文乱码
https://blog.csdn.net/qq_43758789/article/details/120576717 使用RestTemplate发送PostForObject的时候会出现乱码.这是 ...
- java 注解基础
java 内置注解 注解 作用 描述 @Override 将覆盖父类中的方法 作用在子类的方法上 @Deprecated 代码被弃用 使用了被@Deprecated注解的代码则编译器将发出警告 @Su ...
- linux 源码安装 xrdp
前言xrdp可以做两件事,第一件事就是可以使得linux支持RDP协议,使得linux桌面能够通过mstsc连接,第二件事就是RDP代理,我们能通过xrdp连接到其他人的RDP资源 前者,需要编译xr ...
- android audiorecord初始化失败相关资料收集
We're also struggling with audio recording on some Samsung Android Devices. Unfortunately it seems t ...
- (四)用go实现红黑树
本篇文章我们用go来实现红黑树,部分参考资料如下: https://www.cnblogs.com/skywang12345/p/3245399.html#!comments https://blog ...
- (三)用go实现平衡二叉树
本篇,我们用go简单的实现平衡二叉查找树.具体原理参考大佬博客即可:AVL树(一)之 图文解析 和 C语言的实现 1.节点定义 type AVLNode struct{ data int height ...
- ES6-moudle
一.基础认知 结合案例体验module: 前面模拟模块是用立即执行的匿名函数,需要把某些通过window暴露出去,所以还是不能消除全局变量 module模块系统需要服务器环境才能正常执行,在html文 ...
- Systrace学习记录
「置顶」Android 性能优化必知必会[大量文章] https://androidperformance.com/2018/05/07/Android-performance-optimizatio ...
- MySQL连接提示 public key retrieval is not allowed
使用DBeaver连接mysql数据提示public key retrieval is not allowed 修改DBeaver的驱动属性中的allowPublicKeyRetrieval
- 数据仓库服务 GaussDB(DWS)
官网:总体介绍 https://support.huaweicloud.com/dws/index.html 部分源码解读 https://my.oschina.net/gaussdb?tab=new ...