cpp程序使⽤jsoncpp对json格式数据序列化和反序列化操作
1.Json介绍
JSON: JavaScript Object Notation(JavaScript 对象表⽰法),是存储和交换⽂本信息的语法。它类似 XML,但⽐ XML 更⼩、更快,更易解析。
JSON 是轻量级的⽂本数据交换格式,独⽴于语⾔,具有⾃我描述性,更易理解。
JSON ⽂本格式在语法上与创建 JavaScript 对象的代码相同,由于这种相似性,⽆需解析器,JavaScript 程序能够使⽤内建的 eval() 函数,⽤ JSON 数据来⽣成原⽣的 JavaScript 对象。
语法规则
数据在名称/值对中
数据由逗号分隔
⼤括号保存对象
中括号保存数组
例如:
var sites = [
{ “name”:“foo” , “url”:“www.foo” },
{ “name”:“google” , “url”:“le” },
{ “name”:“微博” , “url”:“www.weibo” }
];
2.jsoncpp下载
c++中有多种⽅法可以解析json数据,包括Jsoncpp和Boost库的property_tree。
Jsoncpp是个跨平台的开源库,使⽤的⽐较多。
3.jsoncpp使⽤
#include <stdio.h>
#include <assert.h>
#include
#include
#include
#include
#include “json/json.h”
#pragma comment(lib, “json_vc71_libmt.lib”)
using namespace std;
std::string createJson(void)
{
std::string jsonStr;
Json::Value root, language, mail;
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
language[0] = "C";
language[1] = "Cpp";
root["Language"] = language;
mail["QQ"] = "XiaoMing@qq";
mail["Google"] = "XiaoMing@gmail";
root["E-mail"] = mail;
root["Industry"] = "IT";
// jsonWriter是智能指针, 当jsonWriter被析构时, 它所指向的对象(内存)也被⾃动释放std::unique_ptr<J
son::StreamWriter> wStreamWriter()); jsonWriter->write(root, &os);
jsonStr = os.str();
// ⽆格式化的输出
std::cout << "Json-none:\n" << jsonStr << std::endl;
// 格式化的输出
std::cout << "Json-formatted:\n" << StyledString() << std::endl;
return jsonStr;
}
std::string createJsonArray(void)
{
std::string jsonStr;
Json::Value root, person, language, mail;
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
language[0] = "C";
language[1] = "Cp'p";
person["Language"pp language;
mail["QQ"] = "XiaoMing@qq";
mail["Google"] = "XiaoMing@gmail";
person["E-mail"] = mail;
person["Industry"] = "IT";
root.append(person);
person["Name"] = "Lisi";
person["Age"] = 28;
language[0] = "Chinese";
language[1] = "English";
person["Language"] = language;
mail["QQ"] = "lisi@163";
mail["Google"] = "si.li@gmail";
person["E-mail"] = mail;
person["Industry"] = "Finance";
root.append(person);
// jsonWriter是智能指针, 当jsonWriter被析构时, 它所指向的对象(内存)也被⾃动释放std::unique_ptr<J
son::StreamWriter> wStreamWriter()); jsonWriter->write(root, &os);
jsonStr = os.str();
// ⽆格式化的输出
std::cout << "Json-array-none:\n" << jsonStr << std::endl;
// 格式化的输出
std::cout << "Json-array-formatted:\n" << StyledString() << std::endl; return jsonStr;
}
bool parseJsonFromString(const std::string &jsonStr)
{
if (pty())
{
return false;
}
Json::Value root, language, mail;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> const wCharReader()); res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str()+jsonStr.length(), &root, &errs); if (!res || !pty())
{
std::cout << "parseJsonFromString error! " << errs << std::endl;
return false;
}
std::cout << "root.size() = " << root.size() << std::endl;
std::cout << "Name: " << root["Name"].asString() << std::endl;
std::cout << "Age: " << root["Age"].asInt() << std::endl;
language = root["Language"];
error parse new
std::cout << "Language: ";
for (int i = 0; i < language.size(); ++i)
{
std::cout << language[i].asString() << " ";
}
std::cout << std::endl;
mail = root["E-mail"];
std::cout << "E-mail: " << std::endl;
std::cout << "QQ: " << mail["QQ"].asString() << std::endl;
std::cout << "Google: " << mail["Google"].asString() << std::endl;
std::cout << "Industry: " << root["Industry"].asString() << std::endl;
return true;
}
bool parseJsonArrayFromString(const std::string &jsonStr)
{
if (pty())
{
return false;
}
Json::Value root, person, language, mail;
Json::CharReaderBuilder readerBuilder;
std::unique_ptr<Json::CharReader> const wCharReader()); res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str()+jsonStr.length(), &root, &errs); if (!res || !pty())
{
std::cout << "parseJsonFromString error! " << errs << std::endl;
return false;
}
std::cout << "root.size() = " << root.size() << std::endl;
for(int i = 0; i < root.size(); ++i)
{
person = root[i];
std::cout << "Name: " << person["Name"].asString() << std::endl;
std::cout << "Age: " << person["Age"].asInt() << std::endl;
language = person["Language"];
std::cout << "Language: ";
for (int i = 0; i < language.size(); ++i)
{
std::cout << language[i].asString() << " ";
}
std::cout << std::endl;
mail = person["E-mail"];
std::cout << "E-mail: " << std::endl;
std::cout << "QQ: " << mail["QQ"].asString() << std::endl;
std::cout << "Google: " << mail["Google"].asString() << std::endl;
std::cout << "Industry: " << person["Industry"].asString() << std::endl;
}
return true;
}
void parseJsonArrayFromFile(const char *file)
{
std::string line, jsonStr;
std::ifstream ifs(file);
assert(ifs.is_open());
while(getline(ifs, line))
{
jsonStr.append(line);
}
ifs.close();
parseJsonArrayFromString(jsonStr);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。