json c++ 解析库 jsoncpp 编译和使用

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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值