TA的每日心情 | 开心 7 小时前 |
---|
签到天数: 20 天 [LV.4]偶尔看看III
管理员
- 积分
- 1393
|
在MFC环境下生成JSON串并保存到文件,直接上代码:
头文件:JsonParse.h
#pragma once
#include <vector>
//Json对象基类
class JsonBase {
public:
virtual CString MakeJsonString(int iBlankLen = 0) const = 0;
public:
//功能:获取指定个数的空格
//参数:iLen - 空格个数
//返回值:空格字符串
static CString GetBlankLen(int iLen);
};
//字符串构造类
class JsonString : public JsonBase {
private:
CString value;
public:
JsonString(const CString& str);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//浮点数构造类
class JsonDouble : public JsonBase {
private:
double value;
public:
JsonDouble(double num);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//整数构造类
class JsonNumber : public JsonBase {
private:
int value;
public:
JsonNumber(int num);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//字符串组构造类
class JsonStringArr : public JsonBase {
private:
std::vector<CString> values;
public:
JsonStringArr(std::vector<CString> val);
JsonStringArr();
CString MakeJsonString(int iBlankLen = 0) const override;
};
//对象组构造类
class JsonObjectArr : public JsonBase {
private:
std::vector<JsonBase*> values;
private:
~JsonObjectArr();
public:
JsonObjectArr(std::vector<JsonBase*> val);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//属性组构造类
class JsonObject : public JsonBase {
private:
std::vector<std::pair<CString, JsonBase*>> values;
public:
~JsonObject();
public:
void addValue(const CString& key, JsonBase* value);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//基础对象组构造类
class JsonArrObject : public JsonBase {
private:
std::vector<JsonObject*> values;
public:
~JsonArrObject();
public:
void addValue(JsonObject* value);
CString MakeJsonString(int iBlankLen = 0) const override;
};
//根组构造类
class JsonRootObject : public JsonBase {
private:
std::vector<JsonArrObject*> values;
public:
~JsonRootObject();
public:
void addValue(JsonArrObject* value);
CString MakeJsonString(int iBlankLen = 0) const override;
};
实现文件:JsonParse.cpp
#include "pch.h"
#include "JsonParse.h"
//功能:获取指定个数的空格
//参数:iLen - 空格个数
//返回值:空格字符串
CString JsonBase::GetBlankLen(int iLen)
{
CString strBlank = 采用T("");
for (int i = 0;i < iLen;i++)
{
strBlank += 采用T(" ");
}
return strBlank;
}
//字符串构造类
JsonString::JsonString(const CString& str)
{
value = str;
}
CString JsonString::MakeJsonString(int iBlankLen) const
{
return 采用T("\"") + value + 采用T("\"");
}
//浮点数构造类
JsonDouble::JsonDouble(double num)
{
value = num;
}
CString JsonDouble::MakeJsonString(int iBlankLen) const
{
CString strVal = 采用T("");
strVal.Format(采用T("%.16f"), value);
return strVal;
}
//整数构造类
JsonNumber::JsonNumber(int num)
{
value = num;
}
CString JsonNumber::MakeJsonString(int iBlankLen) const
{
CString strVal = 采用T("");
strVal.Format(采用T("%d"), value);
return strVal;
}
//字符串组构造类
JsonStringArr::JsonStringArr(std::vector<CString> val)
{
values = val;
}
JsonStringArr::JsonStringArr()
{
values.clear();
}
CString JsonStringArr::MakeJsonString(int iBlankLen) const
{
CString ss = 采用T("");
if (values.size() == 0)
{
ss += 采用T("[]");
}
else
{
ss += 采用T("\n") + GetBlankLen(iBlankLen) + 采用T("[\n");
for (size采用t i = 0; i < values.size(); ++i) {
if (i == (values.size() - 1)) {
ss += GetBlankLen(iBlankLen + 1) + 采用T("\"") + values[i] + 采用T("\"\n");
}
else
{
ss += GetBlankLen(iBlankLen + 1) + 采用T("\"") + values[i] + 采用T("\",\n");
}
}
ss += GetBlankLen(iBlankLen) + 采用T("]");
}
return ss;
}
//对象组构造类
JsonObjectArr::~JsonObjectArr()
{
for (int i = 0;i < values.size();i++)
{
delete values[i];
}
values.clear();
}
JsonObjectArr::JsonObjectArr(std::vector<JsonBase*> val)
{
values = val;
}
CString JsonObjectArr::MakeJsonString(int iBlankLen) const
{
CString ss = 采用T("");
ss += GetBlankLen(iBlankLen) + 采用T("[\n");
for (size采用t i = 0; i < values.size(); ++i) {
if (i > 0) {
ss += 采用T(",");
}
ss += GetBlankLen(1) + values[i]->MakeJsonString(iBlankLen + 1);
ss += 采用T(",");
}
ss += GetBlankLen(iBlankLen) + 采用T("]\n");
return ss;
}
//属性组构造类
JsonObject::~JsonObject()
{
for (int i = 0;i < values.size();i++)
{
delete values[i].second;
}
values.clear();
}
void JsonObject::addValue(const CString& key, JsonBase* value)
{
values.push采用back({ key, value });
}
CString JsonObject::MakeJsonString(int iBlankLen) const
{
CString ss = 采用T("");
ss += 采用T("\n") + GetBlankLen(iBlankLen) + 采用T("{\n");
for (size采用t i = 0; i < values.size(); ++i) {
CString strvalue = values[i].second->MakeJsonString(iBlankLen + 1);
ss += GetBlankLen(iBlankLen + 1);
ss += 采用T("\"");
ss += values[i].first;
ss += 采用T("\":");
ss += strvalue;
if (i == (values.size() - 1)) {
ss += 采用T("\n");
}
else
{
ss.TrimRight(采用T("\n"));
ss += 采用T(",\n");
}
}
ss += GetBlankLen(iBlankLen) + 采用T("}");
return ss;
}
//基础对象组构造类
JsonArrObject::~JsonArrObject()
{
for (int i = 0;i < values.size();i++)
{
delete values[i];
}
values.clear();
}
void JsonArrObject::addValue(JsonObject* value)
{
values.push采用back(value);
}
CString JsonArrObject::MakeJsonString(int iBlankLen) const
{
CString ss = 采用T("");
for (int i = 0;i<values.size();i++)
{
CString strvalue = values[i]->MakeJsonString(iBlankLen + 1);
ss += strvalue;
if (i == (values.size() - 1)) {
ss += 采用T("\n");
}
else
{
ss += 采用T(",\n");
}
}
return ss;
}
//根组构造类
JsonRootObject::~JsonRootObject()
{
for (int i = 0;i < values.size();i++)
{
delete values[i];
}
values.clear();
}
void JsonRootObject::addValue(JsonArrObject* value)
{
values.push采用back(value);
}
CString JsonRootObject::MakeJsonString(int iBlankLen) const
{
CString ss = 采用T("");
for (int i = 0;i<values.size();i++)
{
if (iBlankLen > 0)
{
ss += 采用T("\n");
}
ss += GetBlankLen(iBlankLen) + 采用T("[");
CString strvalue = values[i]->MakeJsonString(iBlankLen + 1);
ss += GetBlankLen(iBlankLen) + strvalue;
if (i == (values.size() - 1))
{
ss += GetBlankLen(iBlankLen) + 采用T("]\n");
}
else
{
ss += GetBlankLen(iBlankLen) + 采用T("],\n");
}
}
return ss;
}
示例代码:
// TODO: 在此添加控件通知处理程序代码
JsonRootObject rootObject;
{
JsonArrObject* personTolArr = new JsonArrObject();
{
JsonObject* person = new JsonObject();
person->addValue(采用T("字符串"), new JsonString(采用T("18402")));
person->addValue(采用T("浮点数"), new JsonDouble(850.36558));
person->addValue(采用T("整数"), new JsonNumber(850));
JsonObject* personSon = new JsonObject();
personSon->addValue(采用T("属性1"), new JsonString(采用T("属性值1")));
personSon->addValue(采用T("属性2"), new JsonString(采用T("属性值2")));
personSon->addValue(采用T("属性3"), new JsonString(采用T("属性值3")));
personSon->addValue(采用T("属性4"), new JsonString(采用T("属性值4")));
person->addValue(采用T("属性组"), personSon);
JsonRootObject* personRoot = new JsonRootObject();
JsonArrObject* personArr = new JsonArrObject();
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017847.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017857.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017867.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
personRoot->addValue(personArr);
person->addValue(采用T("点组"), personRoot);
personTolArr->addValue(person);
}
{
JsonObject* person = new JsonObject();
person->addValue(采用T("字符串"), new JsonString(采用T("18402")));
person->addValue(采用T("浮点数"), new JsonDouble(850.36558));
person->addValue(采用T("整数"), new JsonNumber(850));
JsonObject* personSon = new JsonObject();
personSon->addValue(采用T("属性1"), new JsonString(采用T("属性值1")));
personSon->addValue(采用T("属性2"), new JsonString(采用T("属性值2")));
personSon->addValue(采用T("属性3"), new JsonString(采用T("属性值3")));
personSon->addValue(采用T("属性4"), new JsonString(采用T("属性值4")));
person->addValue(采用T("属性组"), personSon);
JsonRootObject* personRoot = new JsonRootObject();
JsonArrObject* personArr = new JsonArrObject();
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017847.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017857.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
{
JsonObject* personItem = new JsonObject();
personItem->addValue(采用T("X"), new JsonDouble(5017867.33040384023));
personItem->addValue(采用T("Y"), new JsonDouble(4373415.5478087282));
personItem->addValue(采用T("Z"), new JsonDouble(0.0));
personArr->addValue(personItem);
}
personRoot->addValue(personArr);
person->addValue(采用T("点组"), personRoot);
personTolArr->addValue(person);
}
//加入根
rootObject.addValue(personTolArr);
}
//获取字符串
CString jsonString = rootObject.MakeJsonString();
//保存到本地文件
char * old采用locale = 采用strdup(setlocale(LC采用ALL, NULL));
setlocale(LC采用ALL, "chs");//设定字符集
CString filePath = 采用T("d:\\测试.json");
CStdioFile fileRet;
if (fileRet.Open(filePath, CStdioFile::modeWrite | CStdioFile::modeCreate | CStdioFile::typeText))
{
fileRet.WriteString(jsonString);
fileRet.Close();
}
setlocale(LC采用ALL, old采用locale);//还原字符集
free(old采用locale);
|
|