C++ CJson解析json数据

原文链接: https://www.cnblogs.com/blackhumour2018/p/9467232.html

字符串转成JSON(其中str为字符串)

Json::Reader Reader;
Json::Value DevJson;
Reader.parse(str,DevJson);
int dev_id = DevJson["dev_id"].asInt();
int index = DevJson["index"].asInt();

JSON转字符串(其中DevStr为字符串)

Json::Value DevJson = DevsJson[i];
std::string DevStr = DevJson.toStyledString();
printf("Msg:%s", DevStr.c_str());

JSON数组解析:

Json::Reader Reader;
 Json::Value DevsJson;
 Reader.parse(MsgStr, DevsJson);
 int siNum = DevsJson.size();
 for(int i=0; i < siNum; i++)
 {
 Json::Value DevJson = DevsJson[i];
 std::string DevStr = DevJson.toStyledString();
 printf("Msg:%s", DevStr.c_str());
 }

数组添加:

Json::Value root;
Json::Value person;
person["name"] = "hello world";
person["age"] = 100;
root.append(person);

你可能感兴趣的:(数据结构)