#include <iostream>
#include <string>
using namespace std;
int main()
{
int saveImageCount = 0;
int rtkEventCount = 0;
string quantity = "ac:quantity";
string strSend ="{\n\t\"" + quantity + "\":" +
"\n\t{\n\t\"" + "imageCount" + "\":" + std::to_string(saveImageCount) + ',' + "\n\t" +
"\"rtk-eventCount\"" + ':' + std::to_string(rtkEventCount) + ',' + "\n" + "\t}\n" +
"}";
cout << strSend << endl;
return 0;
}
程序输出:
{
"ac:quantity":
{
"imageCount":0,
"rtk-eventCount":0,
}
}
以上输出的东西json解析不了,因为最后一组数据多了一个','号。
string strSend ="{\n\t\"" + quantity + "\":" +
"\n\t{\n\t\"" + "imageCount" + "\":" + std::to_string(saveImageCount) + ',' + "\n\t" +
"\"rtk-eventCount\"" + ':' + std::to_string(rtkEventCount) + "\n" + "\t}\n" +
"}";
删除代码中的多余逗号即可。
{
"ac:quantity":
{
"imageCount":0,
"rtk-eventCount":0
}
}