VC2008配置JSON环境
from:
http://blog.csdn.net/ningfuxuan/article/details/7617666,方法一测试过
方法一:直接拷贝源文件。这个方法比较简单,但不推荐,因为不便于项目管理。
- VS2008里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON
- 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz
- 将 jsoncpp-src-0.5.0\include 目录下的json文件夹拷贝至 TestJSON 工程目录下
- 将 jsoncpp-src-0.5.0\src\lib_json 目录下的所有.h, .cpp 文件以及json_valueiterator, json_internalarray, json_internalmap全部拷贝至 TestJSON 工程目录下
- 在VS2008里引入工程目录下刚刚从 jsoncpp-src-0.5.0 导入的文件,如图1
- 在VS2008里新建main.cpp来测试jsoncpp是否可用。代码见文章末尾main.cpp
- 在调试过程中会遇到一些错误,相应改之即可:
- json_reader.cpp 中加入#include "stdafx.h",将#include<json/reader.h>改为#include“json/reader.h”,#include<json/value.h>改为#include "json/value.h";
- json_value.cpp中加入#include "stdafx.h",将#include <json/value.h>改为#include "json/value.h", #include <json/writer.h>改为 #include "json/writer.h";
- json_writer.cpp中加入#include "stdafx.h",将#include <json/writer.h>改为#include "json/writer.h"。
方法二:使用静态链接库
- VS2008里新建一个空的控制台程序(用作测试jsoncpp是否可用),名为: TestJSON
- 解压下载好的文件:jsoncpp-src-0.5.0.tar.gz
- 利用VS2008打开jsoncpp-src-0.5.0\makefiles\vs71目录下的jsoncpp.sln,会出现三个Project:jsontest, lib_json, test_lib_json
- 在lib_json上 右击-->Properties-->Configuration Properties-->C/C++-->Code Generation,注意右侧的Runtime Library的内容,如图2,看完箭头所指的东西就可以点确定,关掉属性页。
- 编译lib_json,显示编译成功后,在jsoncpp-src-0.5.0\build\vs71\debug\lib_json目录下会生成一个json_vc71_libmtd.lib,将这个lib拷贝至TestJSON工程目录下。
- 将jsoncpp-src-0.5.0\include\json目录下的所有.h文件拷贝至TestJSON工程目录下,并在工程Header Files引入.
- 将方法一里的main.cpp添加到工程中,并在工程名上 右击-->Properties-->Configuration Properties-->C/C++-->Code Generation, 将Runtime Library改成图2箭头所示内容。
- 在工程名上 右击-->Properties-->Configuration Properties-->Linker-->Input, 在Additional Dependencies里填写json_vc71_libmtd.lib,然后确定,编译就行了。
- 解析:FROM:http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html
- 1. 从字符串解析json
const char * str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";2. 从文件解析json
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
}
int ReadJsonFromFile( const char * filename)3. 向文件中插入json
{
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;
}
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();
}