-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
实现一个 JSON.parse #40
Comments
JSON.parse() 方法解析一个JSON字符串,可将JSON字符串转化为对象。
|
感觉实现的不太完善,先工作,下午再完善一下
|
B 结果为 :{a: 1, b: "hello"} |
eval |
// 只能处理格式正确且不含空格的json
function jsonParse(json) {
i = 0;
str = json;
return parseValue();
}
function parseValue() {
if(str[i]==='n') {
return parseNull();
} else if(str[i]==='t') {
return parseTrue();
} else if(str[i]==='f') {
return parseFalse();
} else if(str[i]==='"') {
return parseString();
} else if(str[i]==='[') {
return parseArray();
} else if(str[i]==='{') {
return parseObject();
} else {
return parseNumber();
}
}
function parseSpace() {
while(str[i]==' ') {
i++;
}
}
function parseNull() {
let content = str.substring(i, i+4);
// let content = str.substr(i, 4);
if(content==='null') {
i+=4;
return null;
} else {
throw new Error('Unexpected char at pos: ' + i);
}
}
function parseTrue() {
let content = str.substring(i, i+4);
// let content = str.substr(i, 4);
if(content==='true') {
i+=4;
return true;
} else {
throw new Error('Unexpected char at pos: ' + i);
}
}
function parseFalse() {
let content = str.substring(i, i+5);
// let content = str.substr(i, 5);
if(content==='false') {
i+=5;
return false;
} else {
throw new Error('Unexpected char at pos: ' + i);
}
}
function parseString() {
i++;
let result = '';
while(str[i]!=='"') {
result += str[i++];
}
i++;
return result;
}
function parseArray() {
i++;
let result = [];
while(str[i]!==']') {
result.push(parseValue());
if(str[i] ===',') {
i++;
}
}
i++;
return result;
}
function parseObject() {
i++;
let result = {};
while(str[i]!=='}') {
let key = parseString();
i++; // 略过一个冒号(:)
let value = parseValue();
result[key] = value;
if(str[i]===',') i++;
}
i++;
return result;
}
function parseNumber() {
let result = '';
while(isNumberChar(str[i])) {
result += str[i++];
}
return parseFloat(result);
}
function isNumberChar(c) {
let chars = {
'+': true,
'-': true,
'e': true,
'E': true,
'.': true
}
if(chars[c]) {
return true;
}
if(c>='0'&&c<='9') {
return true;
} else {
return false;
}
}
const test = '{"a":1,"b":true,"c":false,"foo":null,"bar":[1,2,3]}';
console.log(jsonParse(test)); |
第一种方式 eval最简单,最直观的方式就是调用 var json = '{"name":"小姐姐", "age":20}';
var obj = eval("(" + json + ")"); // obj 就是 json 反序列化之后得到的对象 直接调用 var rx_one = /^[\],:{}\s]*$/;
var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
if (
rx_one.test(
json
.replace(rx_two, "@")
.replace(rx_three, "]")
.replace(rx_four, "")
)
) {
var obj = eval("(" +json + ")");
}
第二种方式
|
2.利用eval
|
// eval 是最简单的方法,可是不太懂为什么eval中要加个“(”、“)”
var json = '{"name":"小姐姐", "age":20}';
var obj = eval("(" + json + ")"); |
实现一个json.parse() 方式一 例如: var json = '{"name":"蔡徐坤","age":"99“}'
console.log(eval("("+ json +")")) // {"name":"蔡徐坤","age":"99“} 方式二 var jsond = '{"name":"蔡徐坤","age":"99"}'
var result = (new Function('return'+ jsond))()
console.log(result) // |
No description provided.
The text was updated successfully, but these errors were encountered: