// MyJsonTest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <fstream>
#include <cassert>
#include "json/json.h" using namespace std;
/**************************************************************
技术博客
http://www.cnblogs.com/itdef/ 技术交流群
群号码:324164944 欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/ void test1() {
Json::Value root;
Json::FastWriter writer;
Json::Value person; person["FileName"] = "test1";
person["fileLength"] = ""; // 4G以内文件
person.removeMember("fileLength");
root.append(person);
person["FileName"] = "test2";
person["fileLength"] = ""; // 4G以内文件
root.append(person);
person["FileName"] = "test3";
person["fileLength"] = ""; // 4G以内文件
root.append(person); std::string json_file = writer.write(root); ofstream ofs;
ofs.open("test1.json", ios::trunc);
assert(ofs.is_open());
ofs << json_file;
ofs.close();
} int test2() {
Json::Reader pJsonParser;;
string strJson = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":\"24\",\"friends\":[{\"name\":\"chen\",\"sex\":\"男\"},{\"name\":\"li\",\"sex\":\"女\"}]}";
Json::Value tempVal; if (!pJsonParser.parse(strJson, tempVal))
{
cout << "parse error" << endl;
return -;
} string name = tempVal["name"].asString();
string sex = tempVal["sex"].asString();
string age = tempVal["age"].asString(); Json::Value friends = tempVal["friends"];
for (int i = ; i < friends.size(); i++) {
cout << friends[i]["name"].asString() << endl;
}
cout << "name = " << name << " age = " << age << " sex = " << sex << endl; return ;
} void test3() {
Json::Value root;
Json::Value arrayObj;
Json::Value item;
Json::FastWriter writer;
for (int i = ; i<; i++)
{
item["key"] = i;
arrayObj.append(item); //数组项添加
}
root["key1"] = "value1";
root["key2"] = "value2";
root["key3"] = 0x100;
root["key4"] = true;
root["key5"] = "中国人";
item = root["key5"];
root["array"] = arrayObj;
std::cout << root.toStyledString();
std::cout << writer.write(root);
} void test4() {
char* config_doc = "{\"encoding\" : \"UTF-8\",\"plug-ins\" : [\"python\",\"c++\",\"ruby\"],\"indent\" : { \"length\" : 3, \"use_space\" : true }}"; 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 << "Failed to parse configuration\n";
return;
} // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
// such member.
std::string encoding = root.get("encoding", "UTF-8").asString();
// Get the value of the member of root named 'encoding', return a 'null' value if
// there is no such member.
const Json::Value plugins = root["plug-ins"];
for (int index = ; index < plugins.size(); ++index) // Iterates over the sequence elements.
std::cout << plugins[index].asString() << std::endl; std::cout << root["indent"].get("length", ).asInt() << std::endl; std::cout << root["indent"].get("use_space", 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::string text = "{ \"first\": \"James\", \"last\": \"Bond\", \"nums\": [0, 0, 7] }";
std::ofstream outFile; // Parse JSON and print errors if needed
if (!reader.parse(text, root)) {
std::cout << reader.getFormatedErrorMessages();
exit();
}
else { // Read and modify the json data
std::cout << "Size: " << root.size() << std::endl;
std::cout << "Contains nums? " << root.isMember("nums") << std::endl;
root["first"] = "Jimmy";
root["middle"] = "Danger"; // Write the output to a file
outFile.open("output.json");
writer.write(outFile, root);
outFile.close();
}
} int main()
{
test5(); return ;
}

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

代码见

http://www.oschina.net/code/snippet_614253_56645

最新文章

  1. js 循环
  2. Http返回码
  3. HDU 2222 Keywords Search(AC自动机模版题)
  4. Visual Studio 2012完美的拥抱GitHub
  5. CMD魔法堂:CMD进入指定目录
  6. C# 位域[flags] 转
  7. C语言文件操作函数
  8. C++STL 之排列
  9. BFC与hasLayout之间的故事
  10. USB-CSW之旅
  11. Suricata, to 10Gbps and beyond(X86架构)
  12. 8.23.4 IO-输入输出16个流
  13. c#图片裁剪
  14. MWeb for Mac使用教程-如何在文档库中快速搜索
  15. sys模块的介绍
  16. crontab 在指定时间范围每隔2小时执行一次和指定时间执行实例
  17. .NET MVC 学习笔记(六)— 数据导入
  18. iOS 打开系统设置的常用功能
  19. dkhadoop的自然语言处理技术介绍
  20. keras Lambda 层

热门文章

  1. iOS NSLog去掉时间戳及其他输出样式
  2. Idea问题记录
  3. Linux下编写动态链接库
  4. QQ空间点赞代码
  5. hdu 4336 Card Collector——最值反演
  6. 【python】Beautiful Soup的使用
  7. 在Linux中安装Oracle(较详细图解)
  8. nginix.conf 中的gzip模块设置
  9. 解决Python代码编码问题 SyntaxError: Non-UTF-8 code starting with &#39;\xc1&#39;
  10. MySql存储过程简单实例