3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【sessionStorage】1億万回煎じの「JSON文字列⇔オブジェクト」変換

Last updated at Posted at 2022-05-13

JSON文字列とオブジェクト

JavaScriptのオブジェクト(Pythonではdictionary型)とは以下のようにkeyとvalueを持たせたデータ形式で、同じkey名で別の変数として扱うことができる。
各valueには、文字列数値nullbool値オブジェクト配列を入れることができる。

json1 = {
  name: hoge,
  email: hoge@mail,
}
json2 = {
  name: hige,
  email: hige@mail,
}

JSON文字列は上記を文字列にしたもの(valueの型は全て文字列になる)で、sessionStorageなどに格納するときなどに使用する。

JSON文字列→オブジェクト

早速。

let json_data = JSON.parse(sessionStorage.getItem("KeyName"));

オブジェクト→JSON文字列

いよっ!

const jsonString = JSON.stringify(json_data);
sessionStorage.setItem("KeyName", jsonString);
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?