代码之家  ›  专栏  ›  技术社区  ›  VIKRAM

如何在javascript中获得以下结果[关闭]

  •  -3
  • VIKRAM  · 技术社区  · 7 年前
    {
       "DeviceProfile": {
       "-xmlns": "http://www.hp.com/schema/m2m/",
       "Metadata": {
       "Model": "DEMO",
       "OntologyReference": {
        "-xmlns:tns": "http://www.YY.com/schema/m2m/",
        "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
        }
      },
      "AssetParams": [
        {
          "-ParamName": "Device-ID",
          "-DisplayName": "Device-ID",
          "-Mandatory": "true",
          "-ReadOnly": "true",
          "-DataType": "String",
          "-Category": "IoT",
          "-LiveUpdate": "false",
          "-xpath": "'Unit-ID''",
          "-Decoding": "none",
          "-MinLength": "1"
        },
        {
          "-ParamName": "deviceHost",
          "-DisplayName": "Device Host",
          "-Mandatory": "false",
          "-ReadOnly": "false",
          "-DataType": "String",
          "-Category": "IoT",
          "-Decoding": "none"
        }
        ]
        }
        }
    

    如何在javascript中仅获取AssetParams作为json对象,如下所示:

    {"AssetParams": [
        {
          "-ParamName": "Device-ID",
          "-DisplayName": "Device-ID",
          "-Mandatory": "true",
          ....
          ]}
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Jaromanda X    7 年前

    var obj = {
      "DeviceProfile": {
        "-xmlns": "http://www.hp.com/schema/m2m/",
        "Metadata": {
          "Model": "DEMO",
          "OntologyReference": {
            "-xmlns:tns": "http://www.YY.com/schema/m2m/",
            "-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
          }
        },
        "AssetParams": [{
            "-ParamName": "Device-ID",
            "-DisplayName": "Device-ID",
            "-Mandatory": "true",
            "-ReadOnly": "true",
            "-DataType": "String",
            "-Category": "IoT",
            "-LiveUpdate": "false",
            "-xpath": "'Unit-ID''",
            "-Decoding": "none",
            "-MinLength": "1"
          },
          {
            "-ParamName": "deviceHost",
            "-DisplayName": "Device Host",
            "-Mandatory": "false",
            "-ReadOnly": "false",
            "-DataType": "String",
            "-Category": "IoT",
            "-Decoding": "none"
          }
        ]
      }
    };
    
    let {AssetParams} = obj.DeviceProfile;
    let theJsonYouAskedFor = JSON.stringify({AssetParams});
    console.log(theJsonYouAskedFor);