Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use JSON.parse () in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 07:34:35 09月25日 Update

Editor to share with you how to use JSON.parse () in JavaScript. I hope you will get something after reading this article. Let's discuss it together.

JSON.parse () usage feature in JavaScript

When parsing JSON strings, you need to pay attention to some specifications of JSON format, otherwise it will be easy to report errors.

JSON data has strict rules on the type and format of values. The specific rules are as follows:

This method uses string type JSON formatted data.

This method also supports three types of values, namely, numeric, Boolean, and null, and converts them into corresponding literal values.

Other types are not supported.

JSON.parse ('China')

/ / 'China'

JSON.parse (null) / / null

JSON.parse (111.) / / 111

JSON.parse (0x12) / / 18

JSON.parse (true) / / true

JSON.parse ([])

/ / Uncaught SyntaxError: Unexpected end of JSON input

Strings must use double quotation marks, not single quotation marks.

JSON.parse ('"String"')

/ / 'String'

JSON.parse ('\ 'String\')

/ / Uncaught SyntaxError: Unexpected token'in JSON at position 0

Only decimal strings are supported, but numbers must be followed after the decimal point.

JSON.parse ('111') / / 111

JSON.parse ('0x12')

/ / Uncaught SyntaxError: Unexpected token x in JSON at position 1

JSON.parse ('111.232') / / 111.232

JSON.parse ('111.')

/ / Uncaught SyntaxError: Unexpected end of JSON input

Undefined, Symbol, and BigInt cannot be used, and numbers do not support NaN, Infinity, and-Infinity.

JSON.parse (undefined)

/ / Uncaught SyntaxError: Unexpected token u in JSON at position 0

JSON.parse (Symbol ())

/ / Uncaught TypeError: Cannot convert a Symbol value to a string

JSON.parse ('12n')

/ / Uncaught SyntaxError: Unexpected token n in JSON at position 2

Compound types can only be literals such as: [] and {}.

The object constructor cannot be used because it is treated as an execution statement and is not supported.

You cannot use Object and Array, nor can it be functions, RegExp objects, Date objects, Error objects, and so on.

JSON.parse ('[]')

/ / []

JSON.parse ('Object ()')

/ / Uncaught SyntaxError: Unexpected token O in JSON at position 0

The property name of an object must be in double quotation marks.

JSON.parse ('{"key": 1}')

/ / {key: 1}

JSON.parse ('{key: 1}')

/ / Uncaught SyntaxError: Unexpected token k in JSON at position 1

After the last member of an array or object, you cannot add a comma.

JSON.parse ('[1,2,3,4,]')

/ / VM2771:1 Uncaught SyntaxError: Unexpected token] in JSON at position 13

JSON.parse ('{"key": 1,}')

/ / VM2779:1 Uncaught SyntaxError: Unexpected token} in JSON at position 12

Support for unicode escape.

JSON.parse ('{"\ u0066": 333}')

/ / {f: 333}

Some control characters and escape characters are not supported, such as'\ n','\ t' and so on.

JSON.parse ('"\ n')

/ / Uncaught SyntaxError: Unexpected token

Other methods of parsing

There are other ways to convert a json string to a json object (js object value), but not safe code.

Const str ='{"name": "json", "age": 18}'

Const json = JSON.parse (str)

Const json = eval ("(" + str + ")")

Const json = (new Function ("return" + str))

After reading this article, I believe you have a certain understanding of "how to use JSON.parse () in JavaScript". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Object character support string type method quotation mark number format function data article escape China security three code decimal literal done Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Linux vpn Apple