Unexpected token c in JSON at position 0 ?
发布于 4 年前 作者 zhuqiang 12644 次浏览 来自 问答

同样的代码 引用的mqtt.js是一样的 在一个程序中可以用‘ckoff1’直接发送 一个只能发送json格式的数据 发送‘ckoff1’就报以上错误 有没有大佬给点思路 我想发不是json格式的



2 回复

//获取应用实例
const app = getApp();

Page({
  data: {
    clientnull,
    topic: {
      areaTopic'area',
      LEDcontrolTopic'ledcontrol', 
    },
    value: {
      areaAValue:0,
      areaBValue:0,
      areaCValue:0,
      areaDValue:0,
    },
    LED1Value: [{
      LEDlogo'./../images/LED_gray.png',
      ButtonValue'区域1开灯',
      ButtonFlagtrue,
    }],
    LED2Value: [{
      LEDlogo'./../images/LED_gray.png',
      ButtonValue'区域2开灯',
      ButtonFlagtrue,
    }],
    LED3Value: [{
      LEDlogo'./../images/LED_gray.png',
      ButtonValue'区域3开灯',
      ButtonFlagtrue,
    }],
    LED4Value: [{
      LEDlogo'./../images/LED_gray.png',
      ButtonValue'区域4开灯',
      ButtonFlagtrue,
    }]
  },

  onLoadfunction() {
    var that = this;
    this.data.client = app.globalData.client;
    that.data.client.on('connect', that.ConnectCallback);
    that.data.client.on("message", that.MessageProcess);
    that.data.client.on("error", that.ConnectError);
    that.data.client.on("reconnect", that.ClientReconnect);
    that.data.client.on("offline", that.ClientOffline);
    console.log("on load1");
    
  },

  onShowfunction() {
    console.log("on show1");
  },

  onHidefunction() {
    console.log("on hide1");
  },

  onUnloadfunction() {
    console.log("on unload1");
    var that = this;
    that.data.client.end();
  },

    
  LedControlfunction() {
    var that = this;
    
    if (that.data.LED1Value[0].ButtonFlag) {
        that.setData({
          'LED1Value[0].ButtonValue''区域1关灯',
          'LED1Value[0].ButtonFlag'false,
          "LED1Value[0].LEDlogo"'./../images/LED_red.png',
        })
      that.data.client.publish(that.data.topic.LEDcontrolTopic,'ckon1',{
        qos1
      });
    } else {
          that.setData({
            'LED1Value[0].ButtonValue''区域1开灯',
            'LED1Value[0].ButtonFlag'true,
            "LED1Value[0].LEDlogo"'./../images/LED_gray.png',
          })
      that.data.client.publish(that.data.topic.LEDcontrolTopic,'ckoff1',{
        qos1
      });
    }
  },

  LedContro2function() {
    var that = this;
    
    if (that.data.LED2Value[0].ButtonFlag) {
        that.setData({
          'LED2Value[0].ButtonValue''区域2关灯',
          'LED2Value[0].ButtonFlag'false,
          "LED2Value[0].LEDlogo"'./../images/LED_red.png',
        })
        that.data.client.publish(that.data.topic.LEDcontrolTopic, 'ckon2',{
        qos1
        });
    } else {
          that.setData({
            'LED2Value[0].ButtonValue''区域2开灯',
            'LED2Value[0].ButtonFlag'true,
            "LED2Value[0].LEDlogo"'./../images/LED_gray.png',
          })
      that.data.client.publish(that.data.topic.LEDcontrolTopic, 'ckoff2',{
        qos1
      });
    }
  },

  LedContro3function() {
    var that = this;
    
    if (that.data.LED3Value[0].ButtonFlag) {
        that.setData({
          'LED3Value[0].ButtonValue''区域3关灯',
          'LED3Value[0].ButtonFlag'false,
          "LED3Value[0].LEDlogo"'./../images/LED_red.png',
        })
      that.data.client.publish(that.data.topic.LEDcontrolTopic,'ckon3',{
        qos1
      });
    } else {
          that.setData({
            'LED3Value[0].ButtonValue''区域3开灯',
            'LED3Value[0].ButtonFlag'true,
            "LED3Value[0].LEDlogo"'./../images/LED_gray.png',
          })
      that.data.client.publish(that.data.topic.LEDcontrolTopic, 'ckoff3',{
        qos1
      });
    }
  },

  LedContro4function() {
    var that = this;
    
    if (that.data.LED4Value[0].ButtonFlag) {
        that.setData({
          'LED4Value[0].ButtonValue''区域4关灯',
          'LED4Value[0].ButtonFlag'false,
          "LED4Value[0].LEDlogo"'./../images/LED_red.png',
        })
      that.data.client.publish(that.data.topic.LEDcontrolTopic, 'ckon4',{
        qos1
      });
    } else {
          that.setData({
            'LED4Value[0].ButtonValue''区域4开灯',
            'LED4Value[0].ButtonFlag'true,
            "LED4Value[0].LEDlogo"'./../images/LED_gray.png',
          })
      that.data.client.publish(that.data.topic.LEDcontrolTopic, 'ckoff4',{
        qos1
      });
    }
  },

  MessageProcessfunction(topic, payload{
    var that = this;
    //var payload_string = payload.toString();
    //var jsonObj = JSON.parse(Uint8ArrayToString(payload));
    var jsonObj = JSON.parse(payload);
    var payload_areaA = jsonObj.aA;
    var payload_areaB = jsonObj.aB;
    var payload_areaC = jsonObj.aC;
    var payload_areaD = jsonObj.aD;
 
    if (topic == that.data.topic.areaTopic) {
        that.setData({
          'value.areaAValue': payload_areaA,
          'value.areaBValue': payload_areaB,
          'value.areaCValue': payload_areaC,
          'value.areaDValue': payload_areaD,
          })
    }
  },

  ConnectCallbackfunction() {
    var that = this;
    // console.log("connect callback ");
    for (var v in that.data.topic) {
      that.data.client.subscribe(that.data.topic[v], {
        qos1
      });
    }
  },

  ConnectErrorfunction(error{
    console.log(error)
  },

  ClientReconnectfunction() {
    console.log("Client Reconnect")
  },

  ClientOfflinefunction() {
    console.log("Client Offline")
  }

})

你可以包一层

const msg = xxx; // 未知数据类型
let data = {};
try(
  JSON.parse(msg)
)catch{
   // 包一层变成 json
   data = {
     content: sendMsg
  }
}
// 发送 data;
回到顶部