LoginSignup
1
0

More than 3 years have passed since last update.

JavaScript でネストしたJsonをパースする

Last updated at Posted at 2020-06-13

最初に

今回はネストしたJsonの特定の「key」を指定して抽出する方法を展開します。

ターゲット

今回のJsonデータさんです。

test.json
{
  "test": {
     "word": "テスト"
   }, 
 "user": [{
     "id": 520, 
     "name": "Test"
   }]
}

抽出作業

test.js
var testJson = {
  "test": {
     "word": "テスト"
   }, 
 "user": [{
     "id": 520, 
     "name": "Test"
   }]
};

//「"word": "テスト"」を抽出する
var extractionWord = testJson.test.word;
//「"name": "Test"」を抽出する
var extractionId = testJson.user[0].name;
//コンソール出力
console.log(`抽出結果:___word抽出:${extractionWord }___name抽出:${extractionId }`);

↓コンソール画面

抽出結果:___word抽出:テスト___name抽出:Test

最後に

めちゃくちゃ簡単な抽出方法でした。
特定のKeyを全部抽出とかになるともっといい方法があると思います。
今回はあくまで特定のKeyの情報(value)を抽出してくるという簡単な方法でした。

1
0
2

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