2017-08-24 106 views
3

JSON格式我管理一個XML轉換成JSON格式,但現在我需要操縱數據,以便我可以用特定的所需格式操縱與JavaScript

這裏結束了爲原來的形式

{ 
    "machine": "Hassia2", 
    "actual_product_date": "08/24/2017", 
    "holding_tank_table": { 
     "row_0": { 
      "A": "Good Taste", 
      "B": "Slight open", 
      "C": "46", 
     }, 
     "row_1": { 
      "A": "Bad Taste", 
      "B": "Grainy", 
      "C": "125", 
     } 
    }, 
    "packed_product_table": { 
     "row_0": { 
      "D_1": "orange", 
      "D_2": "banana", 
      "D_3": "apple", 
      "E_1": "46", 
      "E_2": "18", 
      "E_3": "1", 
     }, 
     "row_1": { 
      "D_1": "mango", 
      "D_2": "litchy", 
      "D_3": "pinneaple", 
      "E_1": "52", 
      "E_2": "63", 
      "E_3": "1", 
     } 
    }, 
    "hr_qc_table": { 
     "row_0": { 
      "F": "", 
      "G": "" 
     } 
    } 
} 

這裏是需要的形式

{ 
    "machine": "Hassia2", 
    "actual_product_date": "08/24/2017", 
    "holding_tank_table": { 
      "A": [["Good Taste"],["Bad Taste"]], 
      "B": [["Slight open"],["Grainy"]], 
      "C": [["46"],["125"]] 
    }, 
    "packed_product_table": { 
      "D": [["orange","banana","apple"],["mango","litchy","pinneaple"]], 
      "E": [[46,18,1],[52,63,1]] 
    }, 
    "hr_qc_table": { 
      "F": [[""]], 
      "G": [[""]] 
    } 
} 

通知的row_n在原來的形式,我需要的是:

  • 爲具有2個或更多的行作爲父具有2D陣列
  • 的用於具有增量名稱爲具有堆疊在1個陣列

對於它們的值的每個屬性的陣列的每個屬性第二點,我設法使之成爲這樣

var object = { 
 
      "D_1": "orange", 
 
      "D_2": "banana", 
 
      "D_3": "apple", 
 
      "E_1": "46", 
 
      "E_2": "18", 
 
      "E_3": "1", 
 
     }; 
 
    
 
Object.keys(object).forEach(function (k) { 
 
    var parts = k.split(/_(?=\d+$)/); 
 
    if (parts.length === 2) { 
 
     object[parts[0]] = object[parts[0]] || []; 
 
     object[parts[0]][parts[1] - 1] = object[k]; // subtract one for zero based array 
 
     delete object[k]; 
 
    } 
 
}); 
 
console.log(object);

但現在我試圖找到一種方法來實現考慮第二個功能的第一點。

在此先感謝

注: 的名稱和對象的大小已經爲這個例子的目的

+0

因此,行總是有鑰匙row_n,或將這種變化藏漢? –

+0

行總是要row_n @ManuelOtto – Jonathan

回答

-1

另一種方式jQuery的。

// small Object.keys pollyfill 
 
if (!Object.keys) Object.keys = function(o) { 
 
    if (o !== Object(o)) 
 
    throw new TypeError('Object.keys called on a non-object'); 
 
    var k = [], 
 
    p; 
 
    for (p in o) 
 
    if (Object.prototype.hasOwnProperty.call(o, p)) k.push(p); 
 
    return k; 
 
} 
 

 

 
var data = { 
 
    "machine": "Hassia2", 
 
    "actual_product_date": "08/24/2017", 
 
    "holding_tank_table": { 
 
    "row_0": { 
 
     "A": "Good Taste", 
 
     "B": "Slight open", 
 
     "C": "46", 
 
    }, 
 
    "row_1": { 
 
     "A": "Bad Taste", 
 
     "B": "Grainy", 
 
     "C": "125", 
 
    } 
 
    }, 
 
    "packed_product_table": { 
 
    "row_0": { 
 
     "D_1": "orange", 
 
     "D_2": "banana", 
 
     "D_3": "apple", 
 
     "E_1": "46", 
 
     "E_2": "18", 
 
     "E_3": "1", 
 
    }, 
 
    "row_1": { 
 
     "D_1": "mango", 
 
     "D_2": "litchy", 
 
     "D_3": "pinneaple", 
 
     "E_1": "52", 
 
     "E_2": "63", 
 
     "E_3": "1", 
 
    } 
 
    }, 
 
    "hr_qc_table": { 
 
    "row_0": { 
 
     "F": "", 
 
     "G": "" 
 
    } 
 
    } 
 
}; 
 

 
var result = Object.keys(data).reduce((ret, key) => { 
 
    if (typeof data[key] === 'object') { 
 
    ret[key] = extractRows(data[key]); 
 
    } else { 
 
    ret[key] = data[key]; 
 
    } 
 
    return ret; 
 
}, {}); 
 

 
function extractRows(rows) { 
 
    return Object.keys(rows).reduce((ret, key) => { 
 
    const [_, num] = key.split('_'); 
 
    return expandRow(rows[key], ret, num); 
 
    }, {}); 
 
} 
 

 
function expandRow(row, initial, rowNum) { 
 
    return Object.keys(row).reduce((ret, entryKey) => { 
 
    const [key, _] = entryKey.split('_'); 
 
    if (ret[key]) { 
 
     if (!ret[key][rowNum]) { 
 
     ret[key][rowNum] = []; 
 
     } 
 
     ret[key][rowNum].push(row[entryKey]); 
 

 
    } else { 
 
     ret[key] = [ 
 
     [row[entryKey]] 
 
     ]; 
 
    } 
 
    return ret; 
 
    }, initial) 
 
} 
 

 
console.log(result)

+0

爲什麼要投票? – felixmosh

+0

嗨,我測試過,有一個問題,當一個屬性有2個或更多部分組成的單詞(例如:Ab_cd_e) – Jonathan

+0

是的,有代碼分裂,並假設你有** row_n **,你可以嘗試修改我的代碼以滿足您的需求。 – felixmosh

-1

優化這是一個艱難的一個。 我的方法是創建2個函數。第一個(convertRowsToArrays(obj))將行(row_0,row_1等)和子行(D_1,D_2等)轉換爲數組。 第二個函數collapseArrays(obj)將返回的對象從第一個函數轉換爲所需的二維數組。

var json = '{"machine":"Hassia2","actual_product_date":"08/24/2017","holding_tank_table":{"row_0":{"A":"Good Taste","B":"Slight open","C":"46"},"row_1":{"A":"Bad Taste","B":"Grainy","C":"125"}},"packed_product_table":{"row_0":{"D_1":"orange","D_2":"banana","D_3":"apple","E_1":"46","E_2":"18","E_3":"1"},"row_1":{"D_1":"mango","D_2":"litchy","D_3":"pinneaple","E_1":"52","E_2":"63","E_3":"1"}},"hr_qc_table":{"row_0":{"F":"","G":""}}}' 
 

 
var obj = JSON.parse(json) 
 

 
function convertRowsToArrays(obj){ 
 
    var output = {} 
 

 
    var keys = Object.keys(obj) 
 

 
    var enumerations = {} 
 

 
    for(var i=0;i<keys.length;i++){ 
 
     var parts = keys[i].split(/_(?=\d+$)/); 
 
     if(parts.length==2){ 
 
      var key = parts[0] 
 
      var num = parseInt(parts[1]) 
 
      if(!enumerations[key]) 
 
       enumerations[key] = [] 
 
      enumerations[key].push(num) 
 

 
      keys.splice(i,1) 
 
      i-- 
 
     } 
 
    } 
 

 
    for(var key in enumerations){ 
 
     var array = [] 
 
     for(var i=0;i<enumerations[key].length;i++){ 
 
      var val = obj[key+'_'+enumerations[key][i]] 
 
      array.push(typeof val === 'object' ? convertRowsToArrays(val) : val) 
 
     } 
 
     output[key] = array 
 
    } 
 

 

 
    for(i=0;i<keys.length;i++){ 
 
     output[keys[i]] = typeof obj[keys[i]] === 'object' ? convertRowsToArrays(obj[keys[i]]) : obj[keys[i]] 
 
    } 
 

 
    return output 
 
} 
 

 
function collapseArrays(obj){ 
 
    var output = {} 
 

 
    if($.isArray(obj)){ 
 
     var map = {} 
 
     for(var i=0;i<obj.length;i++){ 
 
      for(var key in obj[i]){ 
 
       if(!map[key]) 
 
        map[key] = [] 
 
       map[key].push(obj[i][key]) 
 
      } 
 
     } 
 
     output = map 
 
    }else{ 
 
     for(var key in obj){ 
 
      output[key] = typeof obj[key] === 'object' ? collapseArrays(obj[key]) : obj[key] 
 
     } 
 
    } 
 

 
    return output 
 
} 
 

 
var result = collapseArrays(convertRowsToArrays(obj)) 
 

 
console.log(JSON.stringify(result, null, 2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

調用collapseArrays(convertRowsToArrays(obj))應該給你想要的結果。

注:這就需要在$.isArray()

JSFiddle

+0

使用'$ .isArray()'而不是'Array.isArray()'和刪除jquery的任何原因? – lascort

+0

@lascort向後兼容。他標記了jquery ... –

+0

我不想讓「行」出現 – Jonathan