2015-09-10 85 views
2

我需要一個JSON路徑來在輸入JSON數組的同時返回所有的字段。這就像將數組JSON轉換爲平坦的JSON文件。JSON路徑返回數組JSON輸入中的所有字段?

陣列JSON輸入:

{ 
    "field1": "ALNT12345", 
    "field2": "ALNT345346", 
    "field3": "2015353423", 
    "field4": "2332124343", 
    "arrayfield1": [ 
     { 
      "arr1": "625347", 
      "arr2": "rere" 
     }, 
     { 
      "arr1": "634441", 
      "arr2": "sdfsd" 
     } 
    ] 
} 

如下所示上述陣列JSON必須被轉換成2個記錄。所以,我試圖用json路徑來實現這一點。

所需的輸出:

[ 
    { 
    "field1": "ALNT12345", 
    "field2": "ALNT345346", 
    "field3": "2015353423", 
    "field4": "2332124343", 
    "arr1": "625347", 
    "arr2": "rere" 
    }, 
    { 
    "field1": "ALNT12345", 
    "field2": "ALNT345346", 
    "field3": "2015353423", 
    "field4": "2332124343", 
    "arr1": "634441", 
    "arr2": "sdfsd" 
    } 
] 

我想什麼是JSON路徑,

$ .arrayfield1 *

但只返回數組字段。如果JSON路徑不是實現這一目標的方式,那麼您可以使用javascript代碼來返回所有字段。

謝謝。

回答

1

你可能想試試這個:

var arr = { 
    "field1": "ALNT12345", 
    "field2": "ALNT345346", 
    "field3": "2015353423", 
    "field4": "2332124343", 
    "arrayfield1": [ 
     { 
      "arr1": "625347", 
      "arr2": "rere" 
     }, 
     { 
      "arr1": "634441", 
      "arr2": "sdfsd" 
     } 
    ], 
    "arrayfield2": [ 
     { 
      "arr1": "625347", 
      "arr2": "rere" 
     }, 
     { 
      "arr1": "634441", 
      "arr2": "sdfsd" 
     } 
    ] 
}; 

console.log(conversion(arr)); 

function conversion(arr) { 
    var oneLevel = {}; 
    var multiLevel = []; 
    for(var key in arr) { 
     var item = arr[key]; 
     if(typeof item != "object") { 
      oneLevel[key] = item; 
     } else { 
      multiLevel = multiLevel.concat(item); 
     } 
    } 

    for(var i in multiLevel) { 
     for(var j in oneLevel) { 
      multiLevel[i][j] = oneLevel[j]; 
     } 
    } 

    return multiLevel; 
} 

下面是小提琴鏈接測試

https://fiddle.jshell.net/06zfn55L/2/

0

嘗試類似:

fiddle here

var ob = []; 
data.arrayfield1.forEach(function(item) { 
    var keys = Object.keys(data); 
var e = {}; 
    keys.forEach(function(key){ 
     if(Object.prototype.toString.call(data[key]) != '[object Array]') 
     { 
      e[key] = data[key]; 
     } 
     var subkeys = Object.keys(item) 
     subkeys.forEach(function(_subkeys){ 
       e[_subkeys] = item[_subkeys]; 
     }); 
    }); 
    ob.push(e); 
}); 
0

嘗試利用Array.prototype.map()for in循環,Array.prototype.forEach()

var data = { 
 
    "field1": "ALNT12345", 
 
    "field2": "ALNT345346", 
 
    "field3": "2015353423", 
 
    "field4": "2332124343", 
 
    "arrayfield1": [ 
 
     { 
 
      "arr1": "625347", 
 
      "arr2": "rere" 
 
     }, 
 
     { 
 
      "arr1": "634441", 
 
      "arr2": "sdfsd" 
 
     } 
 
    ] 
 
}; 
 

 
var res = data.arrayfield1.map(function(val, key) { 
 
var obj = {}; 
 
for (var i in data) { 
 
    if (!Array.isArray(data[i])) { 
 
    obj[i] = data[i] 
 
    } 
 
} 
 
    var names = Object.keys(val); 
 
    names.forEach(function(value, index) { 
 
    obj[names[index]] = val[value] 
 
    }) 
 
return obj 
 
}); 
 
    
 
console.log(JSON.stringify(res, null, 4))

0

試試這個: -

var x = { 
    "field1": "ALNT12345", 
    "field2": "ALNT345346", 
    "field3": "2015353423", 
    "field4": "2332124343", 
    "arrayfield1": [ 
     { 
      "arr1": "625347", 
      "arr2": "rere" 
     }, 
     { 
      "arr1": "634441", 
      "arr2": "sdfsd" 
     } 
    ] 
}; 



var arr = []; 

if(x.hasOwnProperty('arrayfield1')) { 
    x.arrayfield1.forEach(function(obj1){ 
     var obj = JSON.parse(JSON.stringify(x)); 
     delete obj.arrayfield1; 
     for(key in obj1) { 
      obj[key] = obj1[key]; 
     } 
     arr.push(obj); 
    }); 
} 

console.log(arr);