从github下载jsoncpp-master
在运行\jsoncpp-master\makefiles\msvc2010目录下jsoncpp.sln
会有3个项目
运行lib_json项目生成lib_json.lib。这个静态库就是我们想要的。
这里要注意的是:“运行lib_json项目前要设置一下c/c++-》代码生成-》运行库以便生成不一样的lib文件”
如果lib要用于MTd环境下,则设置为MTd;如果lib要用于MDd环境下,则设置为MDd。
jsoncpp的使用: http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html
在运行\jsoncpp-master\makefiles\msvc2010目录下jsoncpp.sln
会有3个项目
运行lib_json项目生成lib_json.lib。这个静态库就是我们想要的。
这里要注意的是:“运行lib_json项目前要设置一下c/c++-》代码生成-》运行库以便生成不一样的lib文件”
如果lib要用于MTd环境下,则设置为MTd;如果lib要用于MDd环境下,则设置为MDd。
jsoncpp的使用: http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html
// TestJsoncppCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "include/json/json.h"
#include <fstream>
#include <string>
#pragma comment(lib,"lib_json.lib")
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
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();
}
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;
}
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
//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里所有子元素
{
printf("--------------从字符串读取JSON---------------\n");
std::string upload_id = root["uploadid"].asString(); // 访问节点,upload_id = "UP000000"
int code = root["code"].asInt(); // 访问节点,code = 100
printf("upload_id : %s\ncode : %d \n", upload_id.c_str(), code);
}
//2.从文件解析json
string sTempPath = "test_json.json";
printf("--------------从文件读取JSON---------------\n");
ReadJsonFromFile(sTempPath.c_str());
//3.向文件写入json
WriteJsonData(sTempPath.c_str());
system("pause");
return nRetCode;
}