Json学习笔记
GitHubWeb代码下载
Json文件:test.json
{
"adplaces":
[
{
"str_itemspaceid": "12224",
"i_width": 320,
"i_height": 640
},
{
"str_itemspaceid": "12225",
"i_width": 325,
"i_height": 645
}
],
"i_apt": 0,
"str_ip": "10.1.23.12"
}
Json CPP文件: testjson.cpp
//testjson.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include "./json/json.h"
using namespace std;
bool ParseJsonFromFile(const char* filename);
int main()
{
if(!ParseJsonFromFile("./data/test.json")){
printf("ParseJsonFromFile error.\n");
return 0;
}
return 0;
}
bool ParseJsonFromFile(const char* filename)
{
Json::Reader reader;
Json::Value root;
std::ifstream is;
is.open(filename);
if (reader.parse(is, root))
{
int i_apt = root["i_apt"].asInt();
string str_ip = root["str_ip"].asString();
printf("i_apt:%d, str_ip:%s\n", i_apt, str_ip.c_str());
//adplaces
Json::Value adplaces = root["adplaces"];
int adp_size = adplaces.size();
printf("adp_size: %d\n", adp_size);
for( int i=0; i<adp_size; i++){
string str_itemspaceid = adplaces[i]["str_itemspaceid"].asString();
int i_width = adplaces[i]["i_width"].asInt();
int i_height = adplaces[i]["i_height"].asInt();
printf("adplaces[%d] str_itemspaceid=%s, i_width=%d, i_height=%d\n",
i, str_itemspaceid.c_str(), i_width, i_height);
}
}else{
printf("reader.parse() error.\n");
is.close();
return false;
}
is.close();
return true;
}
Makefile文件:
#Makefile
OBJECT=testjson.o
CPP=g++ -g -std=c++11
IJSONPATH=
LJSON=./json/libjson_linux-gcc-5.3.1_libmt.so
INDEBUG=-I.
LIBFLAG=-lpthread -ldl
all: testjson
$(OBJECT):%.o:%.cpp
$(CPP) ${INDEBUG} ${LIBFLAG} -c $< -o $@
testjson:$(OBJECT)
$(CPP) -o testjson ${OBJECT} ${INDEBUG} ${LIBFLAG} $(LJSON)
clean:
$(RM) *.o testjson
参考:
C++处理JSON示例
C++简单使用Jsoncpp来读取写入json文件
C++解析Json,使用JsonCpp读写Json数据
linux下正确安装jsoncpp