您好,登錄后才能下訂單哦!
1)數據內嵌json
{
? "code" : "SheBeiLiXianGaoJingShangChuan",
? "params" :
?{
???? "alarm_source" : "192.68.1.0",
???? "id" : "234",
???? "remark" : "fire alarm",
???? "time_alarm" : "2017-2-1 21:34:21"
?? }
}
Json::Value jsonCode;
jsonCode["code"] ="SheBeiLiXianGaoJingShangChuan";
?
Json::Value jsonParams;
jsonParams["id"] ="234";
jsonParams["remark"] = "firealarm";
jsonParams["time_alarm"] ="2017-2-1 21:34:21";
jsonParams["alarm_source"] ="192.68.1.0";
?
jsonCode["params"] = jsonParams;
2)數據內嵌字符串數組
{
??? "extra_fields":[
??????? "custom_field_1",
??????? "custom_field_2"
?? ],
??? "surveillance_ids":[
??????? "0"
??? ],
??? "order":{
??????? "timestamp":-1
??? },
??? "hit_condition":{
??????? "hit_similarity":80
??? },
??? "start":0,
??? "limit":100
}
代碼
?Json::Value root;
?Json::Value jsonExtraFields;
?jsonExtraFields.append("custom_field_1");
?jsonExtraFields.append("custom_field_2");
?root["extra_fileds"] = jsonExtraFields;
?Json::Value jsonSurveillanceIds;
?jsonSurveillanceIds.append("0");
?root["surveillance_ids"] = jsonSurveillanceIds;
?Json::Value jsonTimestamp;
?jsonTimestamp["timestamp"] = -1;
?root["order"] = jsonTimestamp;
?Json::Value jsonHitSimilarity;
?jsonHitSimilarity["hit_similarity"] = 80;
?root["hit_condition"] = jsonHitSimilarity;
?root["start"] = 0;
?root["limit"] = 100;
?std::string strResult = root.toStyledString();
3)創建一個標準帶有縮進格式的json文件
void WriteContentToJsonFile()
{
?Json::Value root;
?for (int i = 0; i < 10; i++)
?{
? Json::Value node;
? node["zoom"] = 1;
? root["data"].append(node);
?}
?cout << "直接輸出:" << endl;
?Json::FastWriter fw;
?cout << fw.write(root) << endl ;
//直接輸出的沒有進行換行,查看困難
?cout << "縮進輸出:" << endl;
?Json::StyledWriter sw;
?cout << sw.write(root) << endl ;
?ofstream ofstreamHandle;
?ofstreamHandle.open("fengyuzaitu51cto.json");
?ofstreamHandle << sw.write(root);
?ofstreamHandle.close();
}
注意事項
????Json::Value valueNodeList;
? ? for(int i=0; i <3; i++)
{
????Json::Value nodeChannel;
????nodeChannel["id"] = i;
????valueNodeList.append(nodeChannel);
}
?Json::Value valueRoot;
?std::cout<<valueNodeList.size()<<std::endl;\\輸出3
?valueRoot["channel"] = valueNodeList;
std::cout<<valueNodeList.size()<<std::endl;\\輸出0
進行如上的賦值操作之后,不能再調用valueNodeList訪問數組,例如調用valueNodeList.size訪問到的數據始終返回0,因為數據已經遷移到valueRoot對象中。
????
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。