1 下载 jsoncpp 路径如下:
https://github.com/open-source-parsers/jsoncpp
2. 解压文件
unzip jsoncpp-master.zip
3. 提取所需要的源码数据
include 目录 和 src/lib_json 目录
我这边如下效果:
users@(none):TestCode> tree json json ├── include │ └── json │ ├── allocator.h │ ├── assertions.h │ ├── autolink.h │ ├── config.h │ ├── features.h │ ├── forwards.h │ ├── json.h │ ├── reader.h │ ├── value.h │ ├── version.h │ └── writer.h └── src ├── json_reader.cpp ├── jsonTest.cpp ├── json_tool.h ├── json_value.cpp ├── json_valueiterator.inl ├── json_writer.cpp └── version.h.in 3 directories,18 files
在 src 目录加入自己的Makefile 文件 如下:
vim Makefile
CC=g++ AR=ar SEPARATOR="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" jsonlib=libjsoncpp.a SOURCES = json_reader.cpp json_value.cpp json_writer.cpp INCLUDE = -I../include/ OBJS=$(patsubst %.cpp,%.o,$(SOURCES)) all: clean jsonlib .cpp.o: $(CC) ${OPTIMIZE} -o $@ -c $*.cpp $(INCLUDE) @echo $(SEPARATOR); jsonlib:$(OBJS) $(AR) -r -v $(jsonlib) $(OBJS) rm -rf *.o @echo $(SEPARATOR); clean: rm -f ./$(jsonlib) rm -f ./*.o @echo $(SEPARATOR);
4. 测试:
vim TestJson.cpp
#include <iostream> #include <string> #include "json/json.h" using namespace std; int main(int argc,char *argv[]) { Json::Value root; Json::Value arrayobj; Json::Value item; root["key"] = "value"; for (int i = 0; i < 10; i++) { item["arrkey"] = i; arrayobj.append(item); } root["array"] = arrayobj; std::string out = root.toStyledString(); std::cout << out << std::endl; return 0; }
5 编译测试程序:
g++ TestJson.cpp -o TestJson -I../include/ -L. libjsoncpp.a
6. 运行如下:
{ "array" : [ { "arrkey" : 0 },{ "arrkey" : 1 },{ "arrkey" : 2 },{ "arrkey" : 3 },{ "arrkey" : 4 },{ "arrkey" : 5 },{ "arrkey" : 6 },{ "arrkey" : 7 },{ "arrkey" : 8 },{ "arrkey" : 9 } ],"key" : "value" }
最后代码目录结构:
json ├── include │ └── json │ ├── allocator.h │ ├── assertions.h │ ├── autolink.h │ ├── config.h │ ├── features.h │ ├── forwards.h │ ├── json.h │ ├── reader.h │ ├── value.h │ ├── version.h │ └── writer.h └── src ├── json_reader.cpp ├── jsonTest.cpp ├── json_tool.h ├── json_value.cpp ├── json_valueiterator.inl ├── json_writer.cpp ├── libjsoncpp.a ├── Makefile ├── TestJson ├── TestJson.cpp └── version.h.in 3 directories,22 files
7. 一个读 Json 测试 (TestJsonRead.cpp)
#include "json/json.h" #include <iostream> #include <string> using namespace std; string str = "{\"total\":1,\"toReturn\":[{\"createTime\":\"20080806114526000+0800\",\"sex\":\"先生\"},{\"createTime\":\"201706114526000+0800\",\"sex\":\"女士\"}],\"success\":false}"; int main(int argc,char *argv[]) { Json::Reader reader; Json::Value jv; if (reader.parse(str,jv)) { int total = jv["total"].asInt(); bool success = jv["success"].asBool(); std::cout << "total = " << total << std::endl << "success = " << success << std::endl; size_t len = jv["toReturn"].size(); for (unsigned int i = 0; i < len; i++) { std::string time = jv["toReturn"][i]["createTime"].asString(); std::string sex = jv["toReturn"][i]["sex"].asString(); std::cout << "id : " << i << " createTime : " << time << " sex : " << sex << std::endl; } } else { std::cout << "Json::reader.parse error!" << std::endl; } return 0; }
编译:
gcc TestJsonRead.cpp -o TestJsonRead -I../include libjsoncpp.a -lstdc++
运行:
total = 1 success = 0 id : 0 createTime : 20080806114526000+0800 sex : 先生 id : 1 createTime : 201706114526000+0800 sex : 女士