jsoncpp 源码下载地址 : https://github.com/open-source-parsers/jsoncpp
编译
根目录下执行 cmake . && make
编译好的静态库在lib 目录下
测试代码下载地址: http://download.csdn.net/detail/mtour/8064819
测试代码:
#include "json/json.h"
#include <iostream>
#include <fstream>
using namespace std;
int ParseJsonFromFile(const char* filename)
{
// 解析json用Json::Reader
Json::Reader reader;
// Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array...
Json::Value root;
std::ifstream is;
is.open (filename, std::ios::binary );
if (reader.parse(is, root))
{
std::string code;
if (!root["files"].isNull()) // 访问节点,Access an object value by name, create a null member if it does not exist.
code = root["uploadid"].asString();
// 访问节点,Return the member named key if it exist, defaultValue otherwise.
code = root.get("uploadid", "null").asString();
// 得到"files"的数组个数
int file_size = root["files"].size();
cout<<"file_size: "<<file_size<<endl;
// 遍历数组
for(int i = 0; i < file_size; ++i)
{
Json::Value val_image = root["files"][i]["images"];
int image_size = val_image.size();
for(int j = 0; j < image_size; ++j)
{
std::string type = val_image[j]["type"].asString();
std::string url = val_image[j]["url"].asString();
cout<<"url"<<url<<endl;
}
}
}
is.close();
return 0;
}
int main()
{
ParseJsonFromFile("test.json");
return 0;
}