Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JSON.parse () in JavaScript

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report