Unexpected token c in JSON at position 0 ?
同样的代码 引用的mqtt.js是一样的 在一个程序中可以用‘ckoff1’直接发送 一个只能发送json格式的数据 发送‘ckoff1’就报以上错误 有没有大佬给点思路 我想发不是json格式的
2 回复
//获取应用实例
const app = getApp();
Page({
data: {
client: null,
topic: {
areaTopic: 'area',
LEDcontrolTopic: 'ledcontrol',
},
value: {
areaAValue:0,
areaBValue:0,
areaCValue:0,
areaDValue:0,
},
LED1Value: [{
LEDlogo: './../images/LED_gray.png',
ButtonValue: '区域1开灯',
ButtonFlag: true,
}],
LED2Value: [{
LEDlogo: './../images/LED_gray.png',
ButtonValue: '区域2开灯',
ButtonFlag: true,
}],
LED3Value: [{
LEDlogo: './../images/LED_gray.png',
ButtonValue: '区域3开灯',
ButtonFlag: true,
}],
LED4Value: [{
LEDlogo: './../images/LED_gray.png',
ButtonValue: '区域4开灯',
ButtonFlag: true,
}]
},
onLoad: function() {
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");
},
onShow: function() {
console.log("on show1");
},
onHide: function() {
console.log("on hide1");
},
onUnload: function() {
console.log("on unload1");
var that = this;
that.data.client.end();
},
LedControl: function() {
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',{
qos: 1
});
} 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',{
qos: 1
});
}
},
LedContro2: function() {
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',{
qos: 1
});
} 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',{
qos: 1
});
}
},
LedContro3: function() {
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',{
qos: 1
});
} 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',{
qos: 1
});
}
},
LedContro4: function() {
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',{
qos: 1
});
} 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',{
qos: 1
});
}
},
MessageProcess: function(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,
})
}
},
ConnectCallback: function() {
var that = this;
// console.log("connect callback ");
for (var v in that.data.topic) {
that.data.client.subscribe(that.data.topic[v], {
qos: 1
});
}
},
ConnectError: function(error) {
console.log(error)
},
ClientReconnect: function() {
console.log("Client Reconnect")
},
ClientOffline: function() {
console.log("Client Offline")
}
})