2014-02-19 124 views
1

我需要將我的JSON重新組織爲使用Javascript/Jquery的新結構,但我不知道如何做到這一點。Javascript:重新組織JSON

我現在的JSON看起來像這樣,

{ 
    'dishList': [ 
    { 
    'dishName': 'bla-bla1', 
    'dishNumber': 4, 
    'dishType': 'Type 1' 
    }, 
    { 
    'dishName': 'bla-bla2', 
    'dishNumber': 12, 
    'dishType': 'Type 1' 
    }, 
    { 
    'dishName': 'bla-bla3', 
    'dishNumber': 2, 
    'dishType': 'Type 2' 
    } 
] 
} 

,我需要以下結構:

{ 
'dishList': [ 
    { 
    'dishType': 'Type 1', 
    'dishes': [ 
     { 
     'dishName': 'bla-bla1', 
     'dishNumber': 4 
     }, 
     { 
     'dishName': 'bla-bla2', 
     'dishNumber': 12 
     } 
    ] 
    }, 
    { 
    'dishType': 'Type 2', 
    'dishes': [ 
     { 
     'dishName': 'bla-bla3', 
     'dishNumber': 2 
     } 
     ] 
    } 
] 
} 

任何想法我怎麼能做到這一點?

謝謝。

+4

看看這樣的:[再構築一個JSON(http://stackoverflow.com/questions/21034128/re-structuring-a-json/21035282#21035282)。 – Beterraba

回答

4

下面的代碼將重構它爲這個特定的情況。我相信有一種更有效的方式做到這一點,可能是一個更抽象的方式,以及:

var orig_json = { 
     "dishList": [{ 
      "dishName": "bla-bla1", 
       "dishNumber": 4, 
       "dishType": "Type 1" 
     }, { 
      "dishName": "bla-bla2", 
       "dishNumber": 12, 
       "dishType": "Type 1" 
     }, { 
      "dishName": "bla-bla3", 
       "dishNumber": 2, 
       "dishType": "Type 2" 
     }] 
    }, 
    new_json; 
var reorg = function (data) { 
    var types = [], 
     dishes = []; 
    // pass 1, add distinct types 
    data.dishList.forEach(function (value, index, array) { 
     if (types.indexOf(value.dishType) === -1) { 
      // doesn't yet exist 
      types.push(value.dishType); 
     } 
    }); 
    // for each distinct type 
    types.forEach(function (value, index, array) { 
     // pass two to n, reorganize based on type 
     data.dishList.forEach(function (val, i, a) { 
      if (val.dishType === value) { 
       // matches dishType 
       dishes.push({ 
        "dishName": val.dishName, 
        "dishNumber": val.dishNumber 
       }); 
      } 
     }); 
     // redefine value of current array element 
     array[index] = { 
      "dishType": value, 
      "dishes": dishes 
     }; 
     // reset dishes array 
     dishes = []; 
    }); 
    return { 
     "dishList": types 
    }; 
}; 
new_json = reorg(orig_json); 

這裏有一個小提琴:http://jsfiddle.net/NYhBM/

+0

非常感謝!有用! –

2

試試這個功能:

var f = function(json){ 
    var obj = {}; 

    // put dishes into a temporary object 
    // that has properties named as dish types 
    // and its values are array of dish objects of that type 
    for(var i=0;i<json.dishList.length;i++){ 
     if(!obj[json.dishList[i].dishType]) 
       obj[json.dishList[i].dishType] = []; 
     obj[json.dishList[i].dishType].push({ 
      dishName: json.dishList[i].dishName, 
      dishNumber: json.dishList[i].dishNumber, 
     }) 
    } 
    var res = { 
     dishList: [] 
    } 

    // loop through properties of obj i.e. dish types 
    // and populate the ouptput dishlist. 
    for(var p in obj){ 
     if(obj.hasOwnProperty(p)) 
      res.dishList.push({ 
       dishType: p, 
       dishes: obj[p] 
      }); 
    } 
    return res; 
} 

與您輸入的JSON調用它,它會返回輸出。

+1

也許你可以添加一些關於代碼做什麼的評論? –

+0

當然。我編輯了我的answear。 – kamilkp

0

你必須做一個特設功能/爲此循環。
這在某種程度上你需要做什麼:

// if you return a value in the function below, do this: 
yourJson.dishList = reorganizeDishList(yourJson); 

function reorganizeDishList(yourJson) { 
    var dishList = yourJson.dishList; 
    var tmpJson = {}; // this json will hold {type:[array of properties]} 
    for (var i in dishList) { // loop ALL the dishes 
     var properties = {}; // will hold all properties but type 
     var type = ''; 
     for (var j in dishList[i]) { // loop All the properties of the dish 
      if (j == 'dishType') { 
       type = dishList[i][j]; 
      } else { 
       properties[j] = dishList[i][j]; 
      } 
     } 
     if (!tmpJson[type]) { 
      tmpJson[type] = []; // create array if tmpJson[type] is undefined 
     } 
     tmpJson[type].push(properties); 
    } 
    dishList = []; // clear old dishList 
    for (var i in tmpJson) { 
     // create newly organized array ("list") 
     dishList.push({dishType: i, dishes: tmpJson[i]}); 
    } 
    return dishList; 
    // or `yourJson.dishList = dishList` instead of returning a value 
}