LoginSignup
0
0

More than 3 years have passed since last update.

JSONを楽にJavaScriptObjectへ変換する

Posted at

JSONファイルを読み取って, JavaScript Objectへ変換する関数を実装しようとしました.
行き詰まっていたところ, teratailで質問をするとすぐに回答が付きました. 便利な世の中になりましたね.
https://teratail.com/questions/285284

以下ソースコードです. jQuery依存コードです.

const readjson = path => {
    let jsondata;
    $.ajaxSetup({ async: false }); // Ajax通信を同期通信にする
    $.getJSON(path, data => {
        jsondata = data;
    })
    $.ajaxSetup({ async: true }); // Ajax通信を非同期通信に戻す
    return jsondata;
};

使うときは, [const/let] [変数名] = readjson([jsonのパス])を記述します.

0
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
0
0