1
0

More than 1 year has passed since last update.

JSON.parse()

Posted at

JSON.parse()

JavaScriptの組み込み関数
JSON(JavaScript Object Notation)の文字列をJavaScriptオブジェクトに変換する

JSON : データのシリアル化やデータ交換などで使われる軽量なデータ形式

  • シリアル化
    JavaScriptオブジェクトをJSON形式の文字列に変換すること

  • デシリアル化
    JSON形式の文字列をJavaScriptオブジェクトに変換すること

JSON.parse()

JSON形式の文字列を受け取る

解析

対応するJavaScriptオブジェクトに変換

JSON

  • 文字列 : キーと値をダブルクォートで囲む
  • オブジェクト : 中括弧 {} で囲む
  • 配列 : 角括弧 [] で囲む

JSON.parse()の使用例

const jsonString = '{"name": "John", "age": 30, "isMarried": false}';
const parsedObject = JSON.parse(jsonString);
console.log(parsedObject.name); // "John"
console.log(parsedObject.age); // 30
console.log(parsedObject.isMarried); // false

注意

JSON.parse()は有効なJSON形式の文字列を受け取る必要がある
→ダブルクォートを使用

  • シングルクォートで囲まれた文字列はJSON形式とは見なされない
  • JSON形式の文字列にはコメントが含まれない
  • トリリオンのようなBigIntをサポートしていない

JSON.parse()がよく使用される例
  • APIから取得したJSONデータをJavaScriptで扱う場合
  • LocalStorageやSessionStorageに保存されたJSONデータを取得する場合
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0