Created
April 4, 2014 04:28
-
-
Save luckydrq/9968152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 把key不带引号的JSON字符串转换为JSON对象 | |
* | |
* Example: | |
* '{a:1,b:"b",c:{arr:[{name:"n1",title:"t1"},{name:"n2",title:"t2"}]}}' | |
*/ | |
function parse(s) { | |
s = addQuota(s); | |
function addQuota(s) { | |
if (!~s.indexOf(':')) return s; | |
var start = s[0], end = s[s.length - 1]; | |
return start + s.slice(1, -1).split(',').map(function(t){ | |
var index = t.indexOf(':'); | |
if (index == -1) return t; | |
var key = t.slice(0, index).replace(/([\{\[]*)/, '$1' + '"') + '"'; | |
var value = addQuota(t.slice(index + 1)); | |
return [key, value].join(':') | |
}).join(',') + end; | |
} | |
//console.log(s); | |
return JSON.parse(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment