C++Json 变量的初始化

    xiaoxiao2021-03-25  143

    1、从字符串 解析

    [cpp]  view plain  copy int ParseJsonFromString()   {     const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";        Json::Reader reader;     Json::Value root;     if (reader.parse(str, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素     {       std::string upload_id = root["uploadid"].asString();  // 访问节点,upload_id = "UP000000"       int code = root["code"].asInt();    // 访问节点,code = 100     }     return 0;   }   2、从文件中解析

    数据格式:

    {     "uploadid": "UP000000",     "code": "0",     "msg": "",     "files":     [         {             "code": "0",             "msg": "",             "filename": "1D_16-35_1.jpg",             "filesize": "196690",             "width": "1024",             "height": "682",             "images":             [                 {                     "url": "fmn061/20111118",                     "type": "large",                     "width": "720",                     "height": "479"                 },                 {                     "url": "fmn061/20111118",                     "type": "main",                     "width": "200",                     "height": "133"                 }             ]         }     ] }

    [html]  view plain  copy   int ParseJsonFromFile(const char* filename)   {     // 解析json用Json::Reader     Json::Reader reader;     // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...     Json::Value root;                  std::ifstream is;     is.open (filename, std::ios::binary );       if (reader.parse(is, root))     {       std::string code;       if (!root["files"].isNull())  // 访问节点,Access an object value by name, create a null member if it does not exist.         code = root["uploadid"].asString();              // 访问节点,Return the member named key if it exist, defaultValue otherwise.       code = root.get("uploadid", "null").asString();             // 得到"files"的数组个数       int file_size = root["files"].size();             // 遍历数组       for(int i = 0; i < file_size; ++i)       {         Json::Value val_image = root["files"][i]["images"];         int image_size = val_image.size();         for(int j = 0; j < image_size; ++j)         {           std::string type = val_image[j]["type"].asString();           std::string url = val_image[j]["url"].asString();         }       }     }     is.close();     return 0;   }  

    3、在json结构中插入json

    json中的数组,我一开始理解成跟c++中List,都是用下标去访问,但是这个中间有个问题:比如谁一个json的数字变量a,你要获取第一个数组中的元素给b,不能直接用b=a[0],而是得先定义一个int i=0;b=a[i],不然编译的时候会报错,不知道这是为什么,网上也没找到对应说明,如果哪位大神看到的话留言解答一下吧,谢谢~

    [html]  view plain  copy   Json::Value arrayObj;   // 构建对象   Json::Value new_item, new_item1;   new_item["date"] = "2011-12-28";   new_item1["time"] = "22:30:36";   arrayObj.append(new_item);  // 插入数组成员   arrayObj.append(new_item1); // 插入数组成员   int file_size = root["files"].size();   for(int i = 0; i < file_size; ++i)     root["files"][i]["exifs"] = arrayObj;   // 插入原json中  

    4、输出 

    [html]  view plain  copy   // 转换为字符串(带格式)   std::string out = root.toStyledString();   // 输出无格式json字符串   Json::FastWriter writer;   std::string out2 = writer.write(root);   5、解析数组 

    [html]  view plain  copy   std::string strValue="{\"key1\":\"value1\",\"array\":[{\"key2\":\"value2\"},{\"key2\":\"value3\"},{\"key2\":\"value4\"}]}";          Json::Reader reader;//json解析         Json::Value value;//表示一个json格式的对象         if(reader.parse(strValue,value))//解析出json放到json中区         {             std::string out=value["key1"].asString();             std::cout<<out<<std::endl;             const Json::Value arrayObj=value["array"];//迭代器             for (int i=0; i < arrayObj.size();i++)               {                 out=arrayObj[i]["key2"].asString();                 std::cout<<out;                 if(i!=arrayObj.size()-1)                     std::cout<<std::endl;;             }         }     不含迭代器的方法:

    [html]  view plain  copy   string str = "[{"money":"100"},{"SB":"200"}]";       Json::Reader reader;//json解析         Json::Value value;//表示一个json格式的对象                  if(reader.parse(str,value))//解析出json放到json中区         {             for(int i = 0;i<value.size();i++)       {           out=arrayObj[i]["SB"].asString();        }       }    
    转载请注明原文地址: https://ju.6miu.com/read-10478.html

    最新回复(0)