freeCodeCamp/guide/chinese/javascript/standard-objects/json/json-parse/index.md

1.5 KiB

title localeTitle
JSON Parse JSON Parse

JSON Parse

JSON.parse()方法解析字符串并构造由字符串描述的新对象。

句法:

    JSON.parse(text [, reviver]) 
参数:

text 要解析为JSON的字符串

reviver (可选) 该函数将接收keyvalue作为参数。此函数可用于转换结果值。

以下是如何使用JSON.parse()的示例:

var data = '{"foo": "bar"}'; 
 
 console.log(data.foo); // This will print `undefined` since `data` is of type string and has no property named as `foo` 
 
 // You can use JSON.parse to create a new JSON object from the given string 
 var convertedData = JSON.parse(data); 
 
 console.log(convertedData.foo); // This will print `bar 

Repl.it演示

这是reviver一个例子:

var data = '{"value": 5}'; 
 
 var result = JSON.parse(data, function(key, value) { 
    if (typeof value === 'number') { 
        return value * 10; 
    } 
    return value; 
 }); 
 
 // Original Data 
 console.log("Original Data:", data); // This will print Original Data: {"value": 5} 
 // Result after parsing 
 console.log("Parsed Result: ", result); // This will print Parsed Result:  { value: 50 } 

在上面的示例中,所有数字值都乘以10 - Repl.it Demo

更多信息:

JSON.parse - MDN