Chi*_*han 495 javascript json
就像是:
var jsonString = '{ "Id": 1, "Name": "Coke" }';
//should be true
IsJsonString(jsonString);
//should be false
IsJsonString("foo");
IsJsonString("<div>foo</div>")
Run Code Online (Sandbox Code Playgroud)
解决方案不应包含try/catch.我们中的一些人打开"中断所有错误",他们不喜欢调试器打破那些无效的JSON字符串.
Gum*_*mbo 821
使用JSON解析器,如JSON.parse:
function IsJsonString(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Mat*_* H. 424
我知道这个问题已经迟了3年了,但我觉得自己好像在喋喋不休.
虽然Gumbo的解决方案效果很好,但它并没有处理几个没有引发异常的情况 JSON.parse({something that isn't JSON})
我也更喜欢同时返回解析的JSON,因此调用代码不必再次调用JSON.parse(jsonString).
这似乎适合我的需求:
function tryParseJSON (jsonString){
try {
var o = JSON.parse(jsonString);
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o;
}
}
catch (e) { }
return false;
};
Run Code Online (Sandbox Code Playgroud)
Mic*_*Mic 161
先评论一下.问题是关于不使用try/catch.
如果您不介意使用它,请阅读以下答案.这里我们只JSON使用正则表达式检查一个字符串,它在大多数情况下都有效,而不是所有情况.
浏览https://github.com/douglascrockford/JSON-js/blob/master/json2.js中的 450行
有一个regexp检查有效的JSON,如:
if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
//the json is ok
}else{
//the json is not ok
}
Run Code Online (Sandbox Code Playgroud)
编辑:新版本的json2.js进行了比上面更高级的解析,但仍然基于regexp替换(来自@Mrchief的评论)
moe*_*ool 50
// vanillaJS
function isJSON(str) {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
用法: isJSON({})会的false,isJSON('{}')会的true.
要检查,如果事情是一个Array或Object(解析 JSON):
// vanillaJS
function isAO(val) {
return val instanceof Array || val instanceof Object ? true : false;
}
// ES2015
var isAO = (val) => val instanceof Array || val instanceof Object ? true : false;
Run Code Online (Sandbox Code Playgroud)
用法: isAO({})会的true,isAO('{}')会的false.
小智 22
这是我的工作代码:
function IsJsonString(str) {
try {
var json = JSON.parse(str);
return (typeof json === 'object');
} catch (e) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
kuk*_*kko 20
我用一个非常简单的方法检查一个字符串它是如何有效的JSON.
function testJSON(text){
if (typeof text!=="string"){
return false;
}
try{
JSON.parse(text);
return true;
}
catch (error){
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
具有有效JSON字符串的结果:
var input='["foo","bar",{"foo":"bar"}]';
testJSON(input); // returns true;
Run Code Online (Sandbox Code Playgroud)
结果是一个简单的字符串;
var input='This is not a JSON string.';
testJSON(input); // returns false;
Run Code Online (Sandbox Code Playgroud)
带有对象的结果:
var input={};
testJSON(input); // returns false;
Run Code Online (Sandbox Code Playgroud)
空输入的结果:
var input=null;
testJSON(input); // returns false;
Run Code Online (Sandbox Code Playgroud)
最后一个返回false,因为null变量的类型是object.
这适用于每次.:)
Ifi*_*Ifi 15
在原型js中我们有方法isJSON.试试看
http://api.prototypejs.org/language/string/prototype/isjson/
甚至http://www.prototypejs.org/learn/json
"something".isJSON();
// -> false
"\"something\"".isJSON();
// -> true
"{ foo: 42 }".isJSON();
// -> false
"{ \"foo\": 42 }".isJSON();
Run Code Online (Sandbox Code Playgroud)
这里也是打字稿版本:
JSONTryParse(input: any) {
try {
//check if the string exists
if (input) {
var o = JSON.parse(input);
//validate the result too
if (o && o.constructor === Object) {
return o;
}
}
}
catch (e: any) {
}
return false;
};
Run Code Online (Sandbox Code Playgroud)
我参加聚会已经太晚了。这就是我最终所做的。使用快速正则表达式预检查可以大幅提高性能
if(/^\s*(\{|\[)/.test(str)){
try{
JSON.parse(str)
// do something here, or return obj/true
}catch(e){
// do nothing or return false
}
}
Run Code Online (Sandbox Code Playgroud)
正则表达式将检查字符串是否以[或开头{。这将消除大多数错误案例(不是全部)。这是为您提供的快速性能测试https://jsbench.me/awl6fgn8jb/1
最坏的情况下,这可能比try直接使用慢 10-15%,最坏的情况意味着所有字符串都是有效的 json 字符串。
最好的情况是比 pure 快 99% try,最好的情况意味着所有字符串都是无效的 json。
这只查找解析为对象或数组的字符串。请注意,像“true”这样的字符串化 js-premitive 值是有效的 JSON 字符串,为了简单起见,我故意忽略它们。如需全面的预检查,请根据您的用例添加额外的检查。
小智 6
isValidJsonString - 检查有效的 json 字符串
JSON 数据类型 - 字符串、数字、对象(JSON 对象)、数组、布尔值、空值(https://www.json.org/json-en.html)
javascript 中的假值 - false, 0, -0, 0n, ", null, undefined, NaN - ( https://developer.mozilla.org/en-US/docs/Glossary/Falsy )
JSON.parse
适用于 number 、 boolean 、 null 并且有效的 json String 不会引发任何错误。请参考下面的例子
解析 undefined 、对象、数组等时中断
function isValidJsonString(jsonString){
if(!(jsonString && typeof jsonString === "string")){
return false;
}
try{
JSON.parse(jsonString);
return true;
}catch(error){
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
也许它会有用:
function parseJson(code)
{
try {
return JSON.parse(code);
} catch (e) {
return code;
}
}
function parseJsonJQ(code)
{
try {
return $.parseJSON(code);
} catch (e) {
return code;
}
}
var str = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}";
alert(typeof parseJson(str));
alert(typeof parseJsonJQ(str));
var str_b = "c";
alert(typeof parseJson(str_b));
alert(typeof parseJsonJQ(str_b));
Run Code Online (Sandbox Code Playgroud)
输出:
IE7:字符串,对象,字符串,字符串
铬:对象,对象,字符串,字符串
这个答案降低了 trycatch 语句的成本。
我使用 JQuery 来解析 JSON 字符串,并使用 trycatch 语句来处理异常,但是为不可解析的字符串抛出异常会减慢我的代码速度,所以我使用简单的 Regex 来检查字符串是否是可能的 JSON 字符串,而不会出现问题通过检查它的语法,然后我通过使用 JQuery 解析字符串来使用常规方法:
if (typeof jsonData == 'string') {
if (! /^[\[|\{](\s|.*|\w)*[\]|\}]$/.test(jsonData)) {
return jsonData;
}
}
try {
jsonData = $.parseJSON(jsonData);
} catch (e) {
}
Run Code Online (Sandbox Code Playgroud)
我将前面的代码包装在一个递归函数中来解析嵌套的 JSON 响应。
从原型框架String.isJSON定义这里
/**
* String#isJSON() -> Boolean
*
* Check if the string is valid JSON by the use of regular expressions.
* This security method is called internally.
*
* ##### Examples
*
* "something".isJSON();
* // -> false
* "\"something\"".isJSON();
* // -> true
* "{ foo: 42 }".isJSON();
* // -> false
* "{ \"foo\": 42 }".isJSON();
* // -> true
**/
function isJSON() {
var str = this;
if (str.blank()) return false;
str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@');
str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
return (/^[\],:{}\s]*$/).test(str);
}
Run Code Online (Sandbox Code Playgroud)
所以这是可以用来传递字符串对象的版本
function isJSON(str) {
if ( /^\s*$/.test(str) ) return false;
str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@');
str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
return (/^[\],:{}\s]*$/).test(str);
}
Run Code Online (Sandbox Code Playgroud)
/**
* String#isJSON() -> Boolean
*
* Check if the string is valid JSON by the use of regular expressions.
* This security method is called internally.
*
* ##### Examples
*
* "something".isJSON();
* // -> false
* "\"something\"".isJSON();
* // -> true
* "{ foo: 42 }".isJSON();
* // -> false
* "{ \"foo\": 42 }".isJSON();
* // -> true
**/
function isJSON() {
var str = this;
if (str.blank()) return false;
str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@');
str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
return (/^[\],:{}\s]*$/).test(str);
}
Run Code Online (Sandbox Code Playgroud)
我想我知道你为什么要避免这种情况。但也许 try & catch !== try & catch。;o) 我想到了这一点:
var json_verify = function(s){ try { JSON.parse(s); return true; } catch (e) { return false; }};
Run Code Online (Sandbox Code Playgroud)
所以你也可以脏剪辑到 JSON 对象,比如:
JSON.verify = function(s){ try { JSON.parse(s); return true; } catch (e) { return false; }};
Run Code Online (Sandbox Code Playgroud)
由于这是尽可能封装的,它可能不会因错误而中断。
| 归档时间: |
|
| 查看次数: |
514414 次 |
| 最近记录: |