1 #include <json/json.h> 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 int main(){ 7 Json::Value root; 8 Json::FastWriter writer; 9 10 int a = 123; 11 int b = 456; 12 int c = 5858; 13 int d = 4; 14 15 16 root["mprotocolType"] = a; 17 root["dstdeviceID"] = b; 18 root["mprotocolID"] = c; 19 root["mver"] = d; 20 21 string receiveDataToRedis = writer.write(root); 22 23 cout << "data: " << receiveDataToRedis << endl; 24 25 26 return 0; 27 28 }
编译:
# g++ test.cpp -I/usr/local/include /usr/local/lib/-ljson_linux-gcc-4.4.5_libmt.a
运行:
#./a.out
输出;
data: {"dstdeviceID":456,"mprotocolID":5858,"mprotocolType":123,"mver":4}
可以发现,jsoncpp的字段排序默认是按照字母的。