如何使用JSON.parse()JavaScript函数解析JSON?

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"
      }
   ]
}
Example JSON Data
Example JSON Data
JSON数据示例

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的变量保存,如下所示。 在此示例中,我们将打印名为firstNamelastNameage已解析变量。

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"
Parse JSON
Parse JSON
解析JSON

解析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"
Parse JSON Array
Parse JSON Array
解析JSON数组

使用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)"
Parse Date Data with JSON
Parse Date Data with JSON
使用JSON解析日期数据

用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"
LEARN MORE  How To Pretty Print and Format XML In Command Line Linux?
了解更多信息如何在命令行Linux中漂亮地打印和格式化XML?

翻译自: https://www.poftut.com/how-to-parse-json-with-json-parse-javascript-function/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值