我有一个代码,通过void的指针将json数据传递给函数。此代码适用于json字符串。
#include <iostream>
#include <string>
struct funcInputs{
bool bVolume;
std::string data;
void read() {
std::cout<<" userdata: "<<data<<std::endl;
};
};
void test(void *input) {
//Read input
funcInputs *inputVar = (reinterpret_cast< funcInputs *> (input));
inputVar->read();
}
int main()
{
std::string s= "{\"lowerThreshold\":200,\"upperThreshold\":1080}";
funcInputs arg ={true,s};
test(reinterpret_cast<void *>(&arg));
}
打印结果:
userdata: {"lowerThreshold":200,"upperThreshold":1080}
现在,如果我的json是一个对象,就像这样:
{"lowerThreshold":200,"upperThreshold":1080}
在将json对象传递给字符串时,我使用了下面的代码
Json::StreamWriterBuilder builder;
builder.settings_["indentation"] = "";
std::string s = Json::writeString(builder, jsonObj);
funcInputs arg ={true,s};
test(reinterpret_cast<void *>(&arg));
这给了我一个糟糕的结果:
userdata: ���hreshold":200,"upperThreshold":1080}
你能帮我纠正这个错误吗?