JSON

jsoncpp学习

字号+ 作者:H5之家 来源:H5之家 2017-08-20 13:00 我要评论( )

jsoncpp学习,// MyJsonTest.cpp : 定义控制台应用程序的入口点。//#include stdafx.h#include fstream#include cassert#include json/json.husing namespace

正文

// MyJsonTest.cpp : 定义控制台应用程序的入口点。 // #include #include <fstream> #include <cassert> #include std; /************************************************************** 技术博客 技术交流群 群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流 **************************************************************/ void test1() { Json::Value root; Json::FastWriter writer; Json::Value person; person[] = ; person[] = ; // 4G以内文件 person.removeMember(); root.append(person); person[] = ; person[] = ; // 4G以内文件 root.append(person); person[] = ; person[] = ; // 4G以内文件 root.append(person); std::string json_file = writer.write(root); ofstream ofs; ofs.open(, ios::trunc); assert(ofs.is_open()); ofs << json_file; ofs.close(); } int test2() { Json::Reader pJsonParser;; ; Json::Value tempVal; if (!pJsonParser.parse(strJson, tempVal)) { cout << << endl; return -1; } ].asString(); ].asString(); ].asString(); Json::Value friends = tempVal[]; for (int i = 0; i < friends.size(); i++) { cout << friends[i][].asString() << endl; } cout << << name << << age << << sex << endl; return 0; } void test3() { Json::Value root; Json::Value arrayObj; Json::Value item; Json::FastWriter writer; for (int i = 0; i<10; i++) { item[] = i; arrayObj.append(item); //数组项添加 } root[] = ; root[] = ; root[] = 0x100; root[] = true; root[] = ; item = root[]; root[] = arrayObj; std::cout << root.toStyledString(); std::cout << writer.write(root); } void test4() { ; Json::Value root; // will contains the root value after parsing. Json::Reader reader; bool parsingSuccessful = reader.parse(config_doc, root); if (!parsingSuccessful) { // report to the user the failure and their locations in the document. std::cout << ; return; } // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no // such member. std::, ).asString(); // Get the value of the member of root named 'encoding', return a 'null' value if Json::Value plugins = root[]; for (int index = 0; index < plugins.size(); ++index) // Iterates over the sequence elements. std::cout << plugins[index].asString() << std::endl; std::cout << root[]., 3).asInt() << std::endl; std::cout << root[]., true).asBool() << std::endl;; // ... // At application shutdown to make the new configuration document: // Since Json::Value has implicit constructor for all value types, it is not // necessary to explicitly construct the Json::Value object: //root["encoding"] = getCurrentEncoding(); //root["indent"]["length"] = getCurrentIndentLength(); //root["indent"]["use_space"] = getCurrentIndentUseSpace(); Json::StyledWriter writer; // Make a new JSON document for the configuration. Preserve original comments. std::string outputConfig = writer.write(root); // You can also use streams. This will put the contents of any JSON // stream at a particular sub-value, if you'd like. //std::cin >> root["subtree"]; // And you can write to a stream, using the StyledWriter automatically. std::cout << root; } void test5() { Json::Reader reader; Json::Value root; Json::StyledStreamWriter writer; std::; std::ofstream outFile; (!reader.parse(text, root)) { std::cout << reader.getFormatedErrorMessages(); exit(1); } else { // Read and modify the json data std::cout << << root.size() << std::endl; std::cout << << root.isMember() << std::endl; root[] = ; root[] = ; // Write the output to a file outFile.open(); writer.write(outFile, root); outFile.close(); } } int main() { test5(); return 0; } View Code

考虑在打包时候使用json记录打包文件信息
但是json似乎不支持64位数据 那么大于4G的文件长度怎么记录?这是个问题

代码见

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 使用PHP json_decode可能遇到的坑与解决方法

    使用PHP json_decode可能遇到的坑与解决方法

    2017-08-20 17:02

  • jsonasp.net asp.net提取多层嵌套json数据的方法

    jsonasp.net asp.net提取多层嵌套json数据的方法

    2017-08-19 17:04

  • JSON使用 JSON JSON数据 JSON教程 JSON使用

    JSON使用 JSON JSON数据 JSON教程 JSON使用

    2017-08-19 16:01

  • nodejs创建json对象,

    nodejs创建json对象,

    2017-08-19 14:04

网友点评