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 + ')') 只需要这一句
随机推荐
- CentOS7下MySQL数据的导入和导出
一.数据导入 (1)进入mysql [root@localhost mysql]# mysql -u root -p (2)转到对应数据库下 mysql> use zenith_star; (3 ...
- 在Github上搭建个人主页
最近试着在github上搭建个人主页,没用github给的模板,用的是自己在网上找到那种类似个人主页的模板,到时候直接上传到仓库里就行了 首先先创建仓库,点击右上角的加号,选择New reposito ...
- 遍历operation
std::ostringstream out; double f8Value; NXOpen::CAM::CAMSetup *camSetup = displayPart->CAMSetup() ...
- 在Excel中创建随机数据集
1.随机小数0-1之间 =RAND() 2.随机整数1-100之间 =RANDBETWEEN(1,100) 3.生成一定比例的随机数0或1 =IF(RAND()>=0.8,1,0) 4.生成一定 ...
- cnpm 安装不上
以管理员身份运行power shell,输入以下命令: set-ExecutionPolicy RemoteSigned 出现选项之后,输入A,回车,在输入 get-ExecutionPolicy 出 ...
- Vue 中的 key 有什么作用?
key 是为 Vue 中 vnode 的唯一标记,通过这个 key,我们的 diff 操作可以更准确.更快速. Vue 的 diff 过程可以概括为:oldCh 和 newCh 各有两个头尾的变量 o ...
- go开发框架推荐
根据自己了解的情况,从易用性和文档完善程度来说,推荐优先考虑使用如下框架: fiber revel echo iris gin beego 以revel作为入门教程,在go项目的根文件夹里执行下面2条 ...
- java中indexOf()获取指定次数的下标
indexOf() :指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 Java中提供了四中查找方法: int indexOf(String str) 返回第一 ...
- python 嵌套对象转为dict
as_dict(self, keys=None, exclude_keys=None): """ 将ORM对象序列化为字典 :param keys: :return: & ...
- faker函数造数据
from faker import Fakerimport timeimport datetimet = time.time()import random# fake= Faker()# fake.n ...