vs引用 第三方库 json的使用

VC2008配置JSON环境

from:

http://blog.csdn.net/ningfuxuan/article/details/7617666,方法一测试过

方法一:直接拷贝源文件。这个方法比较简单,但不推荐,因为不便于项目管理。

  1. VS2008里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON
  2. 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz
  3. 将 jsoncpp-src-0.5.0\include 目录下的json文件夹拷贝至 TestJSON 工程目录下
  4. 将 jsoncpp-src-0.5.0\src\lib_json 目录下的所有.h, .cpp 文件以及json_valueiterator,  json_internalarray,  json_internalmap全部拷贝至 TestJSON 工程目录下
  5. 在VS2008里引入工程目录下刚刚从 jsoncpp-src-0.5.0 导入的文件,如图1
  6. 在VS2008里新建main.cpp来测试jsoncpp是否可用。代码见文章末尾main.cpp
  7. 在调试过程中会遇到一些错误,相应改之即可:
  8. json_reader.cpp 中加入#include "stdafx.h",将#include<json/reader.h>改为#include“json/reader.h”,#include<json/value.h>改为#include "json/value.h";
  9. json_value.cpp中加入#include "stdafx.h",将#include <json/value.h>改为#include "json/value.h", #include <json/writer.h>改为 #include "json/writer.h";
  10. json_writer.cpp中加入#include "stdafx.h",将#include <json/writer.h>改为#include "json/writer.h"。

方法二:使用静态链接库

  1. VS2008里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON
  2. 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz
  3. 利用VS2008打开jsoncpp-src-0.5.0\makefiles\vs71目录下的jsoncpp.sln,会出现三个Project:jsontest, lib_json, test_lib_json
  4. 在lib_json上 右击-->Properties-->Configuration Properties-->C/C++-->Code Generation,注意右侧的Runtime Library的内容,如图2,看完箭头所指的东西就可以点确定,关掉属性页。
  5. 编译lib_json,显示编译成功后,在jsoncpp-src-0.5.0\build\vs71\debug\lib_json目录下会生成一个json_vc71_libmtd.lib,将这个lib拷贝至TestJSON工程目录下。
  6. 将jsoncpp-src-0.5.0\include\json目录下的所有.h文件拷贝至TestJSON工程目录下,并在工程Header Files引入.
  7. 将方法一里的main.cpp添加到工程中,并在工程名上 右击-->Properties-->Configuration Properties-->C/C++-->Code Generation, 将Runtime Library改成图2箭头所示内容。
  8. 在工程名上 右击-->Properties-->Configuration Properties-->Linker-->Input, 在Additional Dependencies里填写json_vc71_libmtd.lib,然后确定,编译就行了。
  9. 解析:FROM:http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html
  10. 1. 从字符串解析json
         const  char * str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";  

        Json::Reader reader;  
        Json::Value root;  
         if  (reader.parse(str, root))   //  reader将Json字符串解析到root,root将包含Json里所有子元素  
        {  
            std:: string  upload_id = root["uploadid"].asString();   //  访问节点,upload_id = "UP000000"  
             int  code = root["code"].asInt();     //  访问节点,code = 100 
        }  
    2. 从文件解析json
    int  ReadJsonFromFile( const  char * filename)  
    {  
        Json::Reader reader; //  解析json用Json::Reader   
        Json::Value root;  //  Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array         

        std::ifstream  is ;  
         is .open (filename, std::ios::binary );    
         if  (reader.parse( is , root, FALSE))  
        {  
            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();  
            
            code = root. get ("uploadid", "null").asString(); //  访问节点,Return the member named key if it exist, defaultValue otherwise.    

             int  file_size = root["files"].size();   //  得到"files"的数组个数  
             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(); 
                    printf("type : %s, url : %s \n", type.c_str(), url.c_str());
                }  
            }  
        }  
         is .close();  

         return  0;  
    3. 向文件中插入json
    void  WriteJsonData( const  char * filename)
    {
        Json::Reader reader;  
        Json::Value root;  //  Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array        

        std::ifstream  is ;  
         is .open (filename, std::ios::binary );    
         if  (reader.parse( is , root))  
        {  
            Json::Value arrayObj;    //  构建对象  
            Json::Value new_item, new_item1;  
            new_item["date"] = "2011-11-11";  
            new_item1["time"] = "11:11:11";  
            arrayObj.append(new_item);   //  插入数组成员  
            arrayObj.append(new_item1);  //  插入数组成员  
             int  file_size = root["files"].size();  
             for ( int  i = 0; i < file_size; ++i)  
                root["files"][i]["exifs"] = arrayObj;    //  插入原json中 
            std:: string  out  = root.toStyledString();  
             //  输出无格式json字符串  
            Json::FastWriter writer;  
            std:: string  strWrite = writer.write(root);
            std::ofstream ofs;
            ofs.open("test_write.json");
            ofs << strWrite;
            ofs.close();
        }  

         is .close();  
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值