JSON is a very popular data format that is mainly used in web applications in order to transmit data in a common format. JSON is an ASCII or non-binary format which is human readable. JavaScript generally used to create, use, consume JSON type data. JSON.parse()
is a function that is used to parse JSON structured data.
JSON是一种非常流行的数据格式,主要在Web应用程序中使用,以便以通用格式传输数据。 JSON是人类可读的ASCII或非二进制格式。 JavaScript通常用于创建,使用和使用JSON类型data. JSON.parse()
data. JSON.parse()
是用于解析JSON结构化数据的函数。
JSON数据示例 (Example JSON Data)
During the JSON parse tutorial, we will use the following example JSON data which contains different types like string, integer, date, array, etc.
在JSON解析教程中,我们将使用以下示例JSON数据,其中包含不同的类型,例如字符串,整数,日期,数组等。
{
"firstName":"Ahmet",
"lastName":"Baydan",
"age":7,
"birth":"2012-01-01",
"address":{
"streetAddress":"21 2nd Street",
"city":"Ankara",
"state":"Ankara",
"postalCode":6543
},
"phoneNumbers":[
{
"type":"home",
"number":"212 555-1234"
},
{
"type":"fax",
"number":"646 555-4567"
}
]
}

JSON.parse()函数语法(JSON.parse() Function Syntax)
parse()
function is provided by JSON
library in order to parse JSON data format. There are two parameters where one is optional.
JSON
库提供了parse()
函数,以解析JSON数据格式。 有两个参数,其中一个是可选的。
JSON.parse(text[, reviver])
text
is the data the function will parse which is in JSON format.text
是函数将解析的数据,采用JSON格式。reviver
is optional parameters where If a function, this prescribes how the value originally produced by parsing is transformed, before being returned.reviver
是可选参数,其中,如果为函数,则规定返回之前如何转换最初由解析产生的值。
This function will return an Object
corresponding to the given JSON text
. If the format of the given text does not comply with the JSON format specification the SyntaxError
exception will be thrown.
此函数将返回与给定JSON text
对应的Object
。 如果给定文本的格式不符合JSON格式规范,则将引发SyntaxError
异常。
解析JSON (Parse JSON)
We can parse simply provided JSON data with the JSON.parse()
function. We will provide the data which is held by the variable named person
like below. In this example, we will print parsed variables named firstName
, lastName
, age
.
我们可以使用JSON.parse()
函数解析简单提供的JSON数据。 我们将提供数据,该数据由名为person
的变量保存,如下所示。 在此示例中,我们将打印名为firstName
, lastName
, age
已解析变量。
var person = '{"firstName":"Ahmet","lastName":"Baydan","age":7,"birth":"2012-01-01"}';
obj = JSON.parse(person);
console.log(obj.firstName);
// Output "Ahmet"
console.log(obj.lastName);
// Output "Baydan"
console.log(obj.age);
// Output "7"

解析JSON数组 (Parse JSON Array)
We can also parse a JSON array with the parse()
function. In this example, we will use an array named names
. We will parse the names
array and use in JavaScript.
我们还可以使用parse()
函数来解析JSON数组。 在此示例中,我们将使用名为names
的数组。 我们将解析names
数组并在JavaScript中使用。
var person = '{"names":[{"name":"Ahmet"},{"name":"Ali"},{"name":"İsmail"}]}';
obj = JSON.parse(person);
console.log(obj.names[0].name);
// Output "Ahmet"
console.log(obj.names[1].name);
// Output "Ali"
console.log(obj.names[2].name);
// Output "İsmail"

使用JSON解析日期数据(Parse Date Data with JSON)
The date is the special type where we have to use it especially by creating reviver
function which will handle data values with Date type in JavaScript programming language. We will create a function that will handle the birth
key specially and convert the JSON date data in JavaScript Date type or format like below.
日期是一种特殊类型,我们必须在其中使用它,特别是通过创建reviver
函数,该函数将使用JavaScript编程语言中的Date类型处理数据值。 我们将创建一个将特别处理birth
密钥的函数,并以如下所示JavaScript Date类型或格式转换JSON日期数据。
var text = '{"name":"Ahmet", "birth":"2013-12-14", "city":"Ankara"}';
var obj = JSON.parse(text, function (key, value) {
if (key == "birth") {
return new Date(value);
} else {
return value;
}
});
console.log(obj.birth);
// Output "Sat Dec 14 2013 02:00:00 GMT+0200 (GMT+03:00)"

用JSON解析函数(Parse Function with JSON)
As JavaScript is an interpreted language we can create and run functions is different ways. We can also represent functions in JSON data and run these functions after parsing them. In this example, we will store the function with the key value hello
and run after parsing it.
由于JavaScript是一种解释型语言,因此我们可以以不同的方式创建和运行函数。 我们还可以在JSON数据中表示函数,并在解析它们后运行这些函数。 在此示例中,我们将使用键值hello
存储该函数,并在解析后运行它。
var text = '{"name":"Ahmet", "age":"function(){return 10;}", "city":"Ankara"}';
var obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
console.log(obj.age());
// Output "Output 10"
翻译自: https://www.poftut.com/how-to-parse-json-with-json-parse-javascript-function/