我想将变量添加到 arduino 中的 json 对象中
i want to add variable into json object in arduino
所以我有一个套接字 io 应用程序,我需要发出 json 但不知道如何将变量插入 json 对象
这个例子来自:https://github.com/timum-viw/socket.io-client#emit
socket.emit("plainString", "\"this is a plain string\"");
socket.emit("jsonObject", "{\"foo\":\"bar\"}");
这是我的代码:
char* variable = "this is a string";
socket.emit("jsonObject", "{\"foo\":\"variable\"}");
套接字 io 的结果
[
"foo":"variable"
]
预期结果:
[
"foo":"this is a string"
]
我找到了答案:
void SendSocket(String socketname,String foo,String bar){
String Data = "{\"_foo\":\"" + foo + "\",\"_bar\":\"" + bar + "\"}";
socket.emit(socketname.c_str(), Data.c_str());
}
下面是如何使用它
SendSocket("jsonObject","this is foo","this is bar");
这是从 Arduino(在服务器中)捕获事件的代码:
socket.on("jsonObject",function data(){
console.log(data);
});
结果将是:
[
_foo:"this is foo",
_bar:"this is bar"
]
所以我有一个套接字 io 应用程序,我需要发出 json 但不知道如何将变量插入 json 对象
这个例子来自:https://github.com/timum-viw/socket.io-client#emit
socket.emit("plainString", "\"this is a plain string\"");
socket.emit("jsonObject", "{\"foo\":\"bar\"}");
这是我的代码:
char* variable = "this is a string";
socket.emit("jsonObject", "{\"foo\":\"variable\"}");
套接字 io 的结果
[
"foo":"variable"
]
预期结果:
[
"foo":"this is a string"
]
我找到了答案:
void SendSocket(String socketname,String foo,String bar){
String Data = "{\"_foo\":\"" + foo + "\",\"_bar\":\"" + bar + "\"}";
socket.emit(socketname.c_str(), Data.c_str());
}
下面是如何使用它
SendSocket("jsonObject","this is foo","this is bar");
这是从 Arduino(在服务器中)捕获事件的代码:
socket.on("jsonObject",function data(){
console.log(data);
});
结果将是:
[
_foo:"this is foo",
_bar:"this is bar"
]