对于我使用的json解析 https://github.com/nlohmann/json
我的代码:
...
#include <algorithm>
#include <vector>
#include <sstream>
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
int main(int argc, char *argv[])
{
// read a JSON file
std::stringstream ss;
std::ifstream i("filee.json");
json j_complete = json::parse(i);
std::vector < int > data_send_to_LED;
for (int i =0; i<j_complete["tablica"].size(); i++){
data_send_to_LED.push_back(j_complete["tablica"][i].get<int>());
}
for (int i =0; i<data_send_to_LED.size(); i++){
cout <<"data send: "<< data_send_to_LED[i]<<endl;
}
json j_vec(data_send_to_LED);
int* pv = &data_send_to_LED[0];
...
n = write(sockfd,pv, data_send_to_LED.size());
}
如何通过串口发送矢量data_send_to_LED作为json?
答案 0 :(得分:0)
不知道代码的所有细节
json j_vec(data_send_to_LED); // this makes the json text?
int* pv = &data_send_to_LED[0]; // hope you don't do anything with data_send_to_LED after this point.
...
n = write(sockfd,pv, data_send_to_LED.size()); // this sends the data as binary
您可能需要做的是获取指向j_vec中的数据的指针,可以是j_vec.data(),大小可能是j_vec.size()。
n = write(sockfd, j_vec.data(), j_vec.size());
大小可能需要+1才能获得零终止。