2013-08-05 119 views
2

我有以下的數組:重構JSON對象

var res = { 
    "status": "Success", 
    "data": [ 
     {"assignedTo":"0", "createdDate":"23-07-2013", "count":"2"}, 
     {"assignedTo":"182398", "createdDate":"01-08-2013", "count":"2"}, 
     {"assignedTo":"182398", "createdDate":"23-07-2013", "count":"2"}, 
     {"assignedTo":"182398", "createdDate":"24-07-2013", "count":"12"}, 
     {"assignedTo":"182398", "createdDate":"22-07-2013", "count":"1"}, 
     {"assignedTo":"182398", "createdDate":"30-07-2013", "count":"4"}, 
     {"assignedTo":"182398", "createdDate":"31-07-2013", "count":"19"}, 
     {"assignedTo":"185271", "createdDate":"24-07-2013", "count":"2"}, 
     {"assignedTo":"185271", "createdDate":"23-07-2013", "count":"1"} 
    ] 
} 

現在我想使從上述一個JSON陣列的data到另一個JSON ,其值將是這樣的:

[ 
    { 
     key: "0", 
     values: [["23-07-2013", 2]] 
    }, 
    { 
     key: "182398", 
     values: [["01-08-2013", 2], 
       ["23-07-2013", 2], 
       ["24-07-2013", 12], 
       ["22-07-2013", 1], 
       ["30-7-2013", 4], 
       ["31-7-2013", 19] 
    }, 
    { 
     key: "185271", 
     values: [["24-07-2013", 2], 
       ["23-07-2013", 1] 
    } 
] 

我已經嘗試過,如下所示:

for (i in res.data) { 
    for (k in res.data[i]) { 
     time_val += "[" + res.data[i]['createdDate'] + "," 
        + res.data[i]['count'] + "],"; 
     cumulative_val += '{key:"' + res.data[i]['assignedTo'] 
          + '",values:'+time_val+'},'; 
    } 
} 

能否請您指導我該怎麼做? 在此先感謝。

+1

您的輸入不是JSON。 JavaScript的!= JSON。而你的代碼不是PHP。 –

+0

你應該注意,你想要實現的結構是無效的JSON ...也不是你的輸入,btw ... – Joum

+0

在JS中創建JSON的更好的方法是創建一個對象或數組並將其轉換爲JSON 'JSON.stringify'。手動構建JSON(與您一樣)容易出錯。例如,鍵必須用JSON中的雙引號括起來,並且在字符串中有'key:'而不是'key':''。如果您根本不想創建JSON,但只需將對象轉換爲對象數組,然後說出來。但那與JSON無關。另外,你有什麼問題?請解釋。 –

回答

1

像這樣的東西在javascript:

var res = {"status":"Success","data":[{"assignedTo":"0","createdDate":"23-07-2013","count":"2"}, 
           {"assignedTo":"182398","createdDate":"01-08-2013","count":"2"}, 
           {"assignedTo":"182398","createdDate":"23-07-2013","count":"2"}, 
          {"assignedTo":"182398","createdDate":"24-07-2013","count":"12"}, 
           {"assignedTo":"182398","createdDate":"22-07-2013","count":"1"}, 
           {"assignedTo":"182398","createdDate":"30-07-2013","count":"4"}, 
           {"assignedTo":"182398","createdDate":"31-07-2013","count":"19"}, 
           {"assignedTo":"185271","createdDate":"24-07-2013","count":"2"}, 
           {"assignedTo":"185271","createdDate":"23-07-2013","count":"1"}] 
} 
//Wanted mixed object 
var temp = []; 
//Store keys, so we do not need to check from temp if key allready exists 
var temp_keys = {}; 
//Loop trough data 
for (var i in res.data) 
{ 
    //Check if key is allready stored in object 
    if (!temp_keys[res.data[i]['assignedTo']]) 
    { 
     //Store new key, and save it''s position 
     temp_keys[res.data[i]['assignedTo']] = temp.length; 
     //Create new array element as new object 
     temp.push(
      { 
       'key' : res.data[i]['assignedTo'], 
       'values': [] 
      } 
     ); 
    } 
    //Save values into correct position 
    temp[temp_keys[res.data[i]['assignedTo']]]['values'].push([res.data[i]['createdDate'], res.data[i]['count']]); 
} 
console.log(temp); 
console.log(JSON.stringify(temp)); 

JSON輸出示例:

[{"key":"0","values":[["23-07-2013","2"]]},{"key":"182398","values":[["01-08-2013","2"],["23-07-2013","2"],["24-07-2013","12"],["22-07-2013","1"],["30-07-2013","4"],["31-07-2013","19"]]},{"key":"185271","values":[["24-07-2013","2"],["23-07-2013","1"]]}] 
+0

而不是保存'temp_keys'中的數組索引,你可以保存對象(你添加到數組中)本身。 –

+0

@FelixKling,是的,我可以,但我沒有這樣做,只是因爲問題是想作爲一個數組輸出。如果我創建temp作爲對象{},那麼我需要將它轉換爲數組,以便按順序json.stringify將給我想要的結果作爲數組。也認爲mabye更容易閱讀,目前的方式不能確定。 – Aivar

+0

非常感謝。沒關係。 – Mausumi

0

我猜測,cumulative_key是在錯誤的循環:

lastKey = res.data[0]['assignedTo']; 
for(i in res.data) { 
    if(res.data[i]['assignedTo'] != last) { 
     cumulative_val += '{key:"'+res.data[i]['assignedTo']+'",values:'+time_val+'},'; 
    } 
    time_val += "["+res.data[i]['createdDate']+","+res.data[i]['count']+"],"; 
} 

你應該想想操縱真正的數組對象,然後做一個JSON編碼後。

+0

感謝您的澄清。其實我必須做所有的js而不是php。 但是,首先有兩個問題,每當我嘗試在for循環外打印cumulative_val的值時,time_val都會重複。 第二件事是,所有的日期和數量應該是groupwise和groupTo assignedTo。 – Mausumi

+0

然後如果你不是在談論它,請不要將[php]標籤放在你的文章中:p你的數組是否按「assignedTo」值排序? –

+0

我會爲這個JSON數據包創建合適的類,在一些數據結構(例如數組)中創建分配構造函數並打包這個類的對象。 – KernelPanic

0

使用這些功能

json_decode 

get_object_vars 
+1

OP正在使用JavaScript,而不是PHP。 –

0

試試這個代碼...

var _root = new Array(); 
     for (i in res.data) { 
      for (k in res.data[i]) { 
       var _child = [res.data[i]['createdDate'], res.data[i]['count']]; 
       var _parent = [{"key":res.data[i]['assignedTo'], "values": _child}]; 
      } 
      _root.push(_parent); 
     } 
     var _JSON_ = JSON.stringify(_root);