按顺序写成jsoncpp(c++)

use*_*197 6 c++ json jsoncpp

考虑以下示例,我的来源是

    Json::Value root;
    root["id"]=0;
            Json::Value text;
            text["first"]="i";
            text["second"]="love";
            text["third"]="you";
    root["text"]=text;
    root["type"]="test";
    root["begin"]=1;
    root["end"]=1;
    Json::StyledWriter writer;
    string strJson=writer.write(root);
    cout<<"JSON WriteTest" << endl << strJson <<endl;
Run Code Online (Sandbox Code Playgroud)

我想我应该按照行的顺序编写 json 字段。相反,结果是:

JSON WriteTest
{
   "begin" : 1,
   "end" : 1,
   "id" : 0,
   "text" : {
      "first" : "i",
      "second" : "love",
      "third" : "you"
   },
   "type" : "test"
}
Run Code Online (Sandbox Code Playgroud)

我想要的json格式是

JSON WriteTest
{
   "id" : 0,
   "text" : {
      "first" : "i",
      "second" : "love",
      "third" : "you"
   },
   "type" : "test"
   "begin" : 1,
   "end" : 1,
}
Run Code Online (Sandbox Code Playgroud)

如何编写 Json 订单?

The*_*ark 4

不,我认为你不能。JsonCpp 将其值保存在 a 中std::map<CZString, Value>,该值始终通过 CZString 比较进行排序。所以它不知道您添加项目的原始顺序。