2017-07-29 103 views
2

我寫這段代碼:HTML獲取文本內碼

<input id="input" onchange="parseFile(this.files); load();" type="file"><br> 
<code id="output"></code> 

功能parseFile()(用書面的NodeJS)正確<code>標籤內可以輸出一些JSON數據。這些信息顯示在顯示器中,然後我需要將它們加載到數組中。所以我稱之爲功能加載:

function load() { 

    var dati = document.getElementById('output').innerHTML; 
    var myObject = JSON.parse(dati); 

    for (var i = 0; i < jsonData.counters.length; i++) { 
    var counter = jsonData.counters[i]; 
    console.log(counter.counter_name); 
    } 

    } 

我該怎麼做?我需要創建一個包含從parseFile函數創建的json數據的數組,該函數寫入<code>。我需要從代碼標籤中提取數據。看起來,如果我改變<code>與其他類似的輸入我看不到結果。

因此,我需要使用<code>標籤。任何想法?我不是節點和JavaScript的專家。


JSON輸出的實施例:

{ "encryptionConstant": 884583747, "dexNo": 143, "heldItemId": 218, "tid": 26197, "sid": 33357, "exp": 65090, "abilityId": 47, "abilityNum": 2, "superTrainingHitsRemaining": 0, "superTrainingBag": 0, "pid": 1664894422, "natureId": 22, "isFatefulEncounter": false, "gender": "M", "formId": 0, "evHp": 0, "evAtk": 22, "evDef": 110, "evSpe": 2, "evSpAtk": 12, "evSpDef": 0, "contestStatCool": 0, "contestStatBeauty": 0, "contestStatCute": 0, "contestStatSmart": 0, "contestStatTough": 0, "contestStatSheen": 0, "hasCircleMarking": false, "hasTriangleMarking": false, "hasSquareMarking": false, "hasHeartMarking": false, "hasStarMarking": false, "hasDiamondMarking": false, "pokerusDuration": 0, "pokerusStrain": 0, "medalData": 0, "ribbonData": 0, "contestMemoryRibbonCount": 0, "battleMemoryRibbonCount": 0, "distributionSuperTrainingFlags": 0, "nickname": "굃㒹", "move1Id": 667, "move2Id": 111, "move3Id": 133, "move4Id": 122, "move1Pp": 10, "move2Pp": 40, "move3Pp": 20, "move4Pp": 30, "move1Ppu": 0, "move2Ppu": 0, "move3Ppu": 0, "move4Ppu": 0, "eggMove1Id": 0, "eggMove2Id": 0, "eggMove3Id": 0, "eggMove4Id": 0, "canDoSecretSuperTraining": false, "ivHp": 5, "ivAtk": 24, "ivDef": 15, "ivSpe": 7, "ivSpAtk": 28, "ivSpDef": 21, "isEgg": false, "isNicknamed": false, "notOt": "굃㒹", "notOtGender": "M", "currentHandlerIsOt": true, "geoLocation1RegionId": 0, "geoLocation1CountryId": 0, "geoLocation2RegionId": 0, "geoLocation2CountryId": 0, "geoLocation3RegionId": 0, "geoLocation3CountryId": 0, "geoLocation4RegionId": 0, "geoLocation4CountryId": 0, "geoLocation5RegionId": 0, "geoLocation5CountryId": 0, "otGender": "M"} 

這基本上是數據的列表中,有不嵌套的數組。這就像:

{ 
"id": value, 
"id2": value2, 
"id3": value3 
} 
+0

基本上我得的內容代碼並解析它 –

+0

您已經使用.innerHTML解析代碼塊的內容,然後解析JSON.parse調用。你需要澄清你的問題。 –

回答

0

可以使用的Array.push方法純JS

array.push(item1, item2, ..., itemX) 

這將是這樣一個循環

function arrayPushTest() { 
    var arrayName = []; 
    for (var i = 1; i <= 3; i++) { 
     arrayName.push(i); 
    } 
    alert(arrayName); 
} 
+0

我需要解析json並將數據獲取到數組中。這不是解決方案。 –

+0

你說你用parseFile解析它 –