加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsoncpp 使用

发布时间:2020-12-16 19:23:14 所属栏目:百科 来源:网络整理
导读:// json.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include string#include iostream#include fstream#include ctime// for time#include cstdlib// for rand srandusing namespace std;#include windows.h// for tickcount64#include "json
// json.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>			
#include <ctime>			// for time
#include <cstdlib>			// for rand srand
using namespace std;

#include <windows.h>		// for tickcount64

#include "json/json.h"

/**@brief low letter*/
static const char* sLetters = "abcdefghijklmnopqrstuvwxyz";
/**@brief low letter length*/
static const int sLen = strlen(sLetters);

/**
 *	describe generate random string for test
 *	param{lenth} size of string to generation
 *	return a random string
 */
string getRandomString(int lenth)
{
	string s(lenth,' ');
	srand(time(0));
	for(int i=0; i<lenth; ++i)
	{
		s += sLetters[rand()%sLen];
	}
	return s;
}

/**
 *	describe save json to local file
 *	param{fn} file name to save
 *	return void
 */
static void SaveToFile(const char* fn)
{
	int i = 0;
	int j = 0;
	Json::Value root;	// json root
	// dd_type,root member keys,which can be got by Value::members function,// you can use Value::isMember to test whether a key is belong to the Value
	static const char* dd_type[] = { "online","local","favorite",NULL};	
	srand(time(0));
	while (dd_type[j])
	{
		Json::Value items(Json::arrayValue);	// json array type
		int cnt = rand()%20 + 100;
		for (int k=0; k<cnt; ++k)
		{
			Json::Value item;	
			item["id"] = i++;
			item["url"] = getRandomString(rand()%10 + 5) + ".com";
			item["read"] = i%2;
			items.append(item);
		}
		root[dd_type[j++]] = items;
	}
	{
		ofstream out(fn,ofstream::out | ofstream::binary);
		if (out.is_open())
		{
			out << root;
			out.close();
		}
	}
}

int _tmain(int argc,_TCHAR* argv[])
{
	LONG64 s = GetTickCount64();
	SaveToFile("out.json");
	cout << "OK,time elapsed: " << GetTickCount64() - s << " ms";
	getchar();
	return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读