JavaScript中的JSON对象修改

时间:2016-12-01 06:51:21

标签: javascript json

有人可以帮我修改JSON对象使用javascript,目前我正在使用角度js并从文件中获取JSON数据。但是我想修改下面的JSON并相应地处理。

当前JSON

<bean id="wsSecurityInterceptor" class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
    <property name="securementActions" value="Encrypt Signature UsernameToken" />

    <!-- UsernameToken -->
    <property name="securementUsername" value="user1234" />
    <property name="securementPassword" value="abc1234" />
    <property name="securementPasswordType" value="PasswordText" />
    <property name="securementUsernameTokenElements" value="Nonce Created" />

    <!-- Signature -->
    <property name="securementSignatureKeyIdentifier" value="X509KeyIdentifier" />
    <property name="securementSignatureUser" value="1" />
    <property name="securementSignatureCrypto" ref="cryptoFactory"/>

    <!-- Encrypt -->
    <property name="securementEncryptionUser" value="1"/>
    <property name="securementEncryptionCrypto" ref="cryptoFactory"/>
</bean>

<bean id="cryptoFactory" class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
    <property name="keyStorePassword" value="abc1234" />
    <property name="keyStoreLocation" value="classpath:/certificate/telco.sample.net.jks" />
</bean>

<!-- Configuring Client -->
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>

<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>net.sample.ws.pricing.model.telco.ServiceRequest</value>
            <value>net.sample.ws.pricing.model.telco.ServiceResponse</value>
        </list>
    </property>
</bean>

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <constructor-arg ref="messageFactory"/>
    <property name="defaultUri" value="https://wsgateway.verizon.com:443/VRDOrderingService_v2.0r1"/>
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="marshaller"/>
    <property name="interceptors">
        <list>
            <ref bean="wsSecurityInterceptor"/>
        </list>
    </property>
</bean>

但是从上面的区域对象中的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"
            }
          ]
        }
      ]
    }
  }
}

2 个答案:

答案 0 :(得分:0)

要获取传感器阵列,您只需在初始对象的.map()上使用account.premise.zone,就像这样:

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 

答案 1 :(得分: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);