前提・実現したいこと
JS、jQueryで開発しています。
APIで取得した以下のJSONデータの配列をJSの以下の配列に入れ直したいです。
試行錯誤したのですが、上手くいかなかったのでご質問させていただきました。
わかる方がいらっしゃいましたらご教授頂けますと幸いです。
JSON
1{ 2 "hits": [ 3 { 4 "groupId": 1, 5 "parentGroupId": "", 6 "itemCode": "", 7 "itemName": "テスト1", 8 "sortOrder": 1 9 }, 10 { 11 "groupId": 2, 12 "parentGroupId": "", 13 "itemCode": "", 14 "itemName": "テスト2", 15 "sortOrder": 2 16 }, 17 { 18 "groupId": 3, 19 "parentGroupId": "", 20 "itemCode": "", 21 "itemName": "テスト3", 22 "sortOrder": 3 23 }, 24 { 25 "groupId": 9, 26 "parentGroupId": "1", 27 "itemCode": "", 28 "itemName": "テスト1-1", 29 "sortOrder": 1 30 }, 31 { 32 "groupId": 11, 33 "parentGroupId": "1", 34 "itemCode": "", 35 "itemName": "テスト1-2", 36 "sortOrder": 7 37 }, 38 { 39 "groupId": 14, 40 "parentGroupId": "1", 41 "itemCode": "", 42 "itemName": "テスト1-3", 43 "sortOrder": 12 44 }, 45 { 46 "groupId": 13, 47 "parentGroupId": "1", 48 "itemCode": "", 49 "itemName": "テスト1-4", 50 "sortOrder": 12 51 }, 52 { 53 "groupId": 1029, 54 "parentGroupId": "9", 55 "itemCode": "10", 56 "itemName": "テスト結果10", 57 "sortOrder": 2 58 }, 59 { 60 "groupId": 1026, 61 "parentGroupId": "9", 62 "itemCode": "20", 63 "itemName": "テスト結果20", 64 "sortOrder": 7 65 }, 66 { 67 "groupId": 1031, 68 "parentGroupId": "9", 69 "itemCode": "30", 70 "itemName": "テスト結果30", 71 "sortOrder": 11 72 }, 73 { 74 "groupId": 1076, 75 "parentGroupId": "9", 76 "itemCode": "", 77 "itemName": "テスト9-1", 78 "sortOrder": 18 79 } 80 ] 81}
js配列
1[ 2 { "id" : JSONのgroupId , "parent" : JSONのparentGroupId, "text" : JSONのitemName }, 3 { "id" : JSONのgroupId , "parent" : JSONのparentGroupId, "text" : JSONのitemName }, 4 ....JSONデータの数だけ格納 5 6]
jsonを取得しているソースが以下になります。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/themes/default/style.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/jstree.min.js"></script> </head> <body> <input id="search" type="button" value="検索" /> <div id="jstree"></div> <script type="text/javascript"> let data1 = []; $('#search').click(function() { let hostUrl = "http://localhost:3000/hits"; $.ajax({ type: 'get', url: hostUrl, dataType : 'json' }).done(function(data){ console.log("success"); data1 = data; console.log(data.itemName); $('#jstree').jstree({ 'core': { 'data' : [ //ここに入れ直した配列を入れたいです。 ] } }) }).fail(function(){ alert('データが無いです'); $('#zip').val(""); }); }); </script> </body> </html>
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/07 06:00
2021/04/07 06:11
2021/04/07 06:12
2021/04/07 06:17 編集
2021/04/07 06:17