Jsoncpp指南
导读本篇文章介绍了 例子1 cmake【version 2.x】版本过低,(引申,linux上通过 问题:如何升级cmake至【3.x】版本? 解答:通过源码安装,为什么呢?因为 sudo ./configure
sudo make
sudo make install
cmake升级
综上,configure & make & make install,是最最基本的步骤。建议查看产品的官方说明和发布文档,好的产品是和“说明书”一起发布的。 jsoncpp编译
jsoncpp读写新建工程目录,包含jsoncpp/include/json/*.h和jsoncpp源文件编译所得的 我的json-notes工程目录如下: .
├── include
│ └── json
│ ├── allocator.h
│ ├── assertions.h
│ ├── autolink.h
│ ├── config.h
│ ├── features.h
│ ├── forwards.h
│ ├── json.h
│ ├── reader.h
│ ├── value.h
│ ├── version.h
│ └── writer.h
├── json.cc
├── lib
│ └── libjsoncpp.a
└── test.json
其中 #include <iostream>
#include <fstream>
using namespace std;
#include "include/json/json.h"
void Display(const Json::Value & value);
int main(void)
{
Json::Value hdu;
Json::Value array;
array.append("Jack");
array.append("Rose");
hdu["name"] = "Sun";
hdu["ID"] = 427;
hdu["Friend"] = array;
hdu["subjuct"]["cs"] = "nice";
// Display Json data
Display(hdu);
// write
// wbuild为构建器,可以定制数据流格式
Json::StreamWriterBuilder wbuilder;
wbuilder["indentation"] = " ";
cout << "'" << Json::writeString(wbuilder,hdu) << "'" << endl;
// read
Json::Value root; // will contain the root value after parsing.
Json::CharReaderBuilder rbuilder;
ifstream test("test.json",ifstream::binary);
string errs;
bool issuccess = Json::parseFromStream(rbuilder,test,&root,&errs);
if ( issuccess)
{
wbuilder["indentation"] = "";
cout << Json::writeString(wbuilder,root) << endl;
}
else
{
// report to the user the failure and their locations in the document.
cout << errs << endl;
}
return 0;
}
void Display(const Json::Value & value)
{
cout << value["name"] << endl;
cout << value["ID"] << endl;
cout << value["Friend"][0] << " " << value["Friend"][1] << endl;
cout << value["subjuct"]["cs"] << endl;
}
test.json {
"my-encoding" : "UTF-8","my-plug-ins" : [ "python","c++","ruby" ],"my-indent" : { "length": 3,"use_space": true } }
执行编译指令 g++ -o json json.cc lib/libjsoncpp.a -std=c++11
执行 "Sun"
427
"Jack" "Rose"
"nice"
'{
"Friend" :
[ "Jack","Rose" ],"ID" : 427,"name" : "Sun","subjuct" :
{ "cs" : "nice" } }'
{"my-encoding":"UTF-8","my-indent":{"length":3,"use_space":true},"my-plug-ins":["python","ruby"]}
总结:
(全文完) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |