JSON的扩展库

parse

        作用:解析JSON的字符串

        参数:json的字符串

        返回值:Object,arrary,string,number,boolean,null中之一

stringify

        作用:将对象转成JSON字符串

        参数:value通常是一个Object对象或者数组

        返回值:一个JSON的字符串

解析数据

        转换报文

   ​

        定义接口

        

        解析报文数据

getDeviceData(){
    let httpRequest = http.createHttp();
    httpRequest.request(
      // 'https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens',
      //网址获取地址:https://console.huaweicloud.com/apiexplorer/#/openapi/IAM/doc?api=KeystoneCreateUserTokenByPassword
      //即华为云IotDa设备接入,API Explorer,统一身份认证服务,获取IAM用户Token(使用密码)
      'https://775405bb33.st1.iotda-app.cn-north-4.myhuaweicloud.com:443/v5/iot/81asdfasdfd43asdf5f0asdf76badsf/devices/{device_id}/shadow',
      {
        method: http.RequestMethod.GET,
        header: {
          'Content-Type': 'application/json',
          'X-Auth-Token':this.access_token
        },
        connectTimeout: 60000,
        readTimeout: 60000,
      }, (err, data) => {
      if (!err) {
        let result=JSON.parse(data.result.toString()) as info
        let shadow=JSON.parse(JSON.stringify(result.shadow)) as shadow_demon[]
        
        this.mes_pm25=shadow[0].reported.properties.PM25+''
        console.log('服务器返回结果:'+shadow[0].reported.properties.PM25)
        this.mes_while=shadow[0].reported.properties.white+''
        console.log('服务器返回结果:'+shadow[0].reported.properties.white)
        this.mes_temp=shadow[0].reported.properties.temp+''
        console.log('服务器返回结果:'+shadow[0].reported.properties.temp)
        this.mes_hum=shadow[0].reported.properties.hum+''
        console.log('服务器返回结果:'+shadow[0].reported.properties.hum)
      } else {
        console.info('Result1_error:' + JSON.stringify(err));
        httpRequest.destroy();
      }
    })
  }

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐