2016-12-01 67 views
0

有人可以幫助我使用javascript修改JSON對象,目前我使用角度js並從文件中獲取JSON數據。但我想修改下面的JSON並相應地進行處理。JavaScript中的JSON對象修改

當前JSON

{ 
    "account": { 
    "premise": { 
     "zone": [ 
     { 
      "id": 1, 
      "name": "Tps John?!? \"':7", 
      "type": "DOOR", 
      "functionType": "ENTRY_EXIT", 
      "sensor": [ 
      { 
       "id": 1, 
       "type": "DRY_CONTACT", 
       "sourceType": "ZIGBEE", 
       "serialNumber": "000d6f00030cdbcf.1", 
       "model": "MCT-320 SMA", 
       "manufacturer": "Visonic", 
       "firmwareVersion": "0x00040008", 
       "hardwareVersion": "1" 
      } 
      ] 
     }, 
     { 
      "id": 2, 
      "name": "Motion Sensor $-*9$+%;47$9 %;:?2", 
      "type": "MOTION", 
      "functionType": "INTERIOR_FOLLOWER", 
      "sensor": [ 
      { 
       "id": 2, 
       "type": "MOTION", 
       "sourceType": "ZIGBEE", 
       "serialNumber": "000d6f0004b2af93.1", 
       "model": "NEXT K85 SMA", 
       "manufacturer": "Visonic", 
       "firmwareVersion": "0x0004000b", 
       "hardwareVersion": "1" 
      } 
      ] 
     } 
     ] 
    } 
    } 
} 

但是從上面的JSON在開發區對象有傳感器的對象,但我想只保留傳感器如下

{ 
    "account": { 
    "premise": { 
     "sensor": [ 
     { 
      "id": 1, 
      "type": "DRY_CONTACT", 
      "sourceType": "ZIGBEE", 
      "serialNumber": "000d6f00030cdbcf.1", 
      "model": "MCT-320 SMA", 
      "manufacturer": "Visonic", 
      "firmwareVersion": "0x00040008", 
      "hardwareVersion": "1" 
     }, 
     { 
      "id": 2, 
      "type": "MOTION", 
      "sourceType": "ZIGBEE", 
      "serialNumber": "000d6f0004b2af93.1", 
      "model": "NEXT K85 SMA", 
      "manufacturer": "Visonic", 
      "firmwareVersion": "0x0004000b", 
      "hardwareVersion": "1" 
     } 
     ] 
    } 
    } 
} 
+0

只是使用'JSON.parse()來'創建一個對象,刪除屬性你不這樣做的一個需要並使用'JSON.stringify()'序列化它' –

回答

0

爲了讓您的傳感器陣列,您只需在初始對象的account.premise.zone上使用.map(),如下所示:

o.account.premise.zone.map(e=>e.sensor)

可以重組完整的對象象下面這樣:

let o = { 
    "account": { 
    "premise": { 
     "zone": [ 
     { 
      "id": 1, 
      "name": "Tps John?!? \"':7", 
      "type": "DOOR", 
      "functionType": "ENTRY_EXIT", 
      "sensor": [ 
      { 
       "id": 1, 
       "type": "DRY_CONTACT", 
       "sourceType": "ZIGBEE", 
       "serialNumber": "000d6f00030cdbcf.1", 
       "model": "MCT-320 SMA", 
       "manufacturer": "Visonic", 
       "firmwareVersion": "0x00040008", 
       "hardwareVersion": "1" 
      } 
      ] 
     }, 
     { 
      "id": 2, 
      "name": "Motion Sensor $-*9$+%;47$9 %;:?2", 
      "type": "MOTION", 
      "functionType": "INTERIOR_FOLLOWER", 
      "sensor": [ 
      { 
       "id": 2, 
       "type": "MOTION", 
       "sourceType": "ZIGBEE", 
       "serialNumber": "000d6f0004b2af93.1", 
       "model": "NEXT K85 SMA", 
       "manufacturer": "Visonic", 
       "firmwareVersion": "0x0004000b", 
       "hardwareVersion": "1" 
      } 
      ] 
     } 
     ] 
    } 
    } 
}; 
o = { 
    account:{ 
    premise:{ 
     sensor: o.account.premise.zone.map(e=>e.sensor) 
    } 
    } 
} 

您可能需要使用的文件數據JSON.parse()將其從字符串首先轉換爲一個對象。小例子:

JSON.parse("{foo:bar") //String 
> { foo:bar } //Object 
+0

偉大的工作轉向映射。許多人忘記它。 +1 –

+0

您正在創建對象,而不是在現有對象中進行編輯。 – Mahi

+0

這個功能已經通過熱門撲克牌進入了我的大腦,我一直使用大型對象。我清楚地記得使用'map()'之前和之後:D – xShirase

0

這不是一個優化的方式,但是這可能是解決

var input = {}; //it is your input Object 
var accountObj = { account: { premise: { sensor : [] }}}; 
input.account.premise.zone.forEach(function(zones) { 
    zones.sensor.forEach(function(sensorObj) { 
     accountObj.account.premise.sensor.push(sensorObj); 
    }); 
}); 
console.log(accountObj); 
+0

謝謝!!在這裏,我們創建一個空對象,並推動值是有辦法我們可以修改現有的。因爲我的原始JSON內部有一些其他對象。 – Batman