jsoncpp 使用详解
来源:互联网 发布:tensorflow 1.2 whl 编辑:程序博客网 时间:2024/05/16 07:57
jsoncpp 使用详解
jsoncpp 主要包含三种类型的 class:Value、Reader、Writer。jsoncpp 中所有对象、类名都在 namespace Json 中,包含 json.h 即可。
Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 Adapt 类来适配。
编译问题:
error C1083: 无法打开编译器生成的文件: “../../build/vs71/release/lib_json\json_reader.asm”: No such file or directory
解决方法:
修改静态库的工程属性-->C++-->输出文件-->汇编程序输出-->无列表。
从字符串中读取
#include "include/json/json.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
//字符串
const char* str = "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
"\"born\":-100,\"died\":-44}";
Json::Reader reader;
Json::Value root;
//从字符串中读取数据
if (reader.parse(str, root))
{
string praenomen = root["praenomen"].asString();
string nomen = root["nomen"].asString();
string cognomen = root["cognomen"].asString();
int born = root["born"].asInt();
int died = root["died"].asInt();
cout << praenomen + " " + nomen +" " + cognomen
<< " was born in year " << born
<< ", died in year " << died << endl;
}
return 0;
}
从文件中读取
// 从文件中读取
Json::Reader reader;
Json::Value root;
ifstream is;
is.open("PersonalInfo.json",ios::binary);
if (reader.parse(is, root))
{
// 读取根节点信息
string name = root["name"].asString();
int age = root["age"].asInt();
bool sex_is_male = root["sex_is_male"].asBool();
cout << "My name is " << name << endl;
cout << "I'm " << age <<" years old" << endl;
cout << "I'm a " << (sex_is_male ?"man" : "woman") << endl;
//读取子节点信息
string partner_name = root["partner"]["partner_name"].asString();
int partner_age = root["partner"]["partner_age"].asInt();
bool partner_sex_is_male = root["partner"]["partner_sex_is_male"].asBool();
cout << "My partner's name is " << partner_name << endl;
cout << (partner_sex_is_male ? "he" : "she") << " is "
<< partner_age << " years old" << endl;
//读取数组信息
cout << "Here's my achievements:" << endl;
for (int i = 0; i < root["achievement"].size(); i++)
{
string ach = root["achievement"][i].asString();
cout << ach << '\t';
}
cout << endl;
cout << "Reading Complete!" << endl;
}
写入文件
// 写入文件
// 根节点
Json::Value root;
//根节点属性
root["name"] = Json::Value("Tsybius");
root["age"] = Json::Value(23);
root["sex_is_male"] = Json::Value(true);
// 子节点
Json::Value partner;
//子节点属性
partner["partner_name"] = Json::Value("Galatea");
partner["partner_age"] = Json::Value(21);
partner["partner_sex_is_male"] = Json::Value(false);
// 子节点挂到根节点上
root["partner"] = Json::Value(partner);
// 数组形式
root["achievement"].append("ach1");
root["achievement"].append("ach2");
root["achievement"].append("ach3");
//直接输出
cout << "FastWriter:" << endl;
Json::FastWriter fw;
cout << fw.write(root) << endl << endl;
//缩进输出
cout << "StyledWriter:" << endl;
Json::StyledWriter sw;
cout << sw.write(root) << endl << endl;
//输出到文件
ofstream os;
os.open("PersonalInfo.json");
os << sw.write(root);
os.close();
- jsoncpp 使用详解
- Centos下 jsoncpp使用详解
- Jsoncpp使用详解以及链接问题解决
- jsoncpp详解
- Jsoncpp使用
- JsonCPP 使用
- JSONCPP使用
- 使用jsoncpp
- jsoncpp使用
- jsoncpp使用
- JsonCpp使用
- jsoncpp使用
- JsonCpp 使用
- JSONcpp使用.
- jsoncpp用法详解
- JsonCpp使用方法详解
- JsonCpp库使用
- C++ JsonCpp 使用
- 使用Visual调试库检测内存泄露
- Java+mysql用户注册登录
- DLL开发的问题
- 【PAT】1048. Find Coins
- convert 3D matrix into diagonal block matrix
- jsoncpp 使用详解
- LeetCode Game of Life 解题
- 学习hibernate_02_双向onetoone注解
- C++ SQL 语句格式化
- Windows DLL开发笔记
- c++思考题
- Wuha
- 2016书单总结--看透SpringMvc源代码分析与实践-概述
- 排序