微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jsoncpp的生成和使用

从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



// 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,array...         

	std::ifstream is;
	is.open(filename,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;
}

原文地址:https://www.jb51.cc/json/289847.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐