C++Json生成

简介: C++Json生成

参考文章   乐之者101


写此文章目的在于记录,使用时请尊重原著


采用系统编码,系统采用utf8字符集的话拼接起来字符串就是utf-8了

// BeiChenJson.h
#pragma once
#include "json.h"
#include "json_helper.h"
// 本类为将对象转换为Json格式
// @ 类名: BeiChenjson
// @ author: 杨星
// @ adddresss: yangxing2297@163.com
// @ Version: V1.0.0
// @ Time: 2016-11-8
class BeiChenJson
{
public:
  BeiChenJson();
  virtual ~BeiChenJson();
  // 添加一个value为整数的键值对
  void AddElementInt(const char * key, int value);
  // 添加一个value为字符串的键值对
  void AddElementStr(const char * key, const char * value);
  // 返回拼接好的字符串
  char * toStr(void){
    json_tree_to_string(m_pRoot, &m_pText); 
    return m_pText;
  }
  // 添加一个数组
  void AddArr(){ m_pArr = json_new_array(); }
  // 添加数组元素
  void AddElementArr(BeiChenJson * json)
  {
    json_insert_child(m_pArr, json->m_pRoot);
  }
  // 添加数组对应key值,数据结束
  void AddArrEnd(const char * key)
  {
    json_insert_pair_into_object(m_pRoot, key, m_pArr);
  }
private:
  json_t * m_pRoot;
  json_t * m_pKey;
  json_t * m_pValue;  
  json_t* m_pArr;
  char m_buf[1024]; // 此处内存可以申请小点;
  char* m_pText;
};


类的实现:

// BeiChenJson.cpp
#include "BeiChenJson.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
BeiChenJson::BeiChenJson()
{
  m_pRoot = json_new_object();
  m_pKey = NULL;
  m_pValue = NULL;
}
BeiChenJson::~BeiChenJson()
{
  json_free_value(&m_pRoot);
}
// 添加一个value为整数的键值对
void BeiChenJson::AddElementInt(const char * key, int value)
{
  m_pKey = json_new_string(key);
  memset(m_buf, 0, 1024);
  sprintf_s(m_buf, "%d", value);
  m_pValue = json_new_number(m_buf);
  json_insert_child(m_pKey, m_pValue);
  json_insert_child(m_pRoot, m_pKey);
}
// 添加一个value为字符串的键值对
void BeiChenJson::AddElementStr(const char * key, const char * value)
{
  m_pKey = json_new_string(key);
  m_pValue = json_new_string(value);
  json_insert_child(m_pKey, m_pValue);
  json_insert_child(m_pRoot, m_pKey);
}


测试代码:

// main.cpp
#include <iostream>
#include <string>
#include "BeiChenJson.h"
int main(int argc, char *argv[])
{
  BeiChenJson js1;
  js1.AddElementInt("潘强强", 22);
  BeiChenJson js2;
  js2.AddElementStr("xu wei", "ni hao");
  js2.AddElementInt("heng ji", 10);
  BeiChenJson js3;
  js3.AddElementStr("dd", "nini");
  BeiChenJson js;
  js.AddElementInt("xinglang", 1000);
  js.AddArr();
  js.AddElementArr(&js1);
  js.AddElementArr(&js2);
  BeiChenJson jsa[5];
  for (int i = 0; i < 5; ++i)
  {
    jsa[i].AddElementInt("Code", 12);
    js.AddElementArr(&jsa[i]);
  }// for
  js.AddElementArr(&js3);
  js.AddArrEnd("数组测试列表");
  printf("%s\n", js.toStr());
  return 0;
}


执行后结果:


BeiChenJson类及测试代码均为 乐之者101 的源码,我只是复制下来在vs2013中编译一次,其中修改了一点兼容性问题;引用源码请尊重原著

相关文章
|
1月前
|
JSON 程序员 数据格式
深入探索 “JSON for Modern C++“:安装、构建与应用
深入探索 “JSON for Modern C++“:安装、构建与应用
41 0
|
1月前
|
JSON JavaScript 前端开发
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
C++ 智能指针与 JSON 处理:高级编程技巧与常见问题解析
271 0
|
28天前
|
JSON C++ 数据格式
【C++】Visual Studio C++使用配置Json库文件(老爷式教学)
【C++】Visual Studio C++使用配置Json库文件(老爷式教学)
|
1月前
|
存储 JSON 安全
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
【C++ JSON库 json值的创建手段】深入探究C++中JSON对象定位与操作:从引用到回调函数
67 0
|
1月前
|
存储 JSON 算法
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
C++ JSON库 nlohmann::basic_json::boolean_t 的用法
35 0
|
1月前
|
JSON JavaScript 数据格式
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
【深入探究C++ JSON库】解析JSON元素的层级管理与遍历手段
100 2
|
1月前
|
XML JSON API
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
深入解析C++ JSON库:nlohmann::json:: parse的内部机制与应用
56 0
|
1月前
|
存储 JSON 算法
C++ JSON库 nlohmann::basic_json::binary_t的用法
C++ JSON库 nlohmann::basic_json::binary_t的用法
27 0
|
1月前
|
JSON 数据格式 C++
C++ JSON库 nlohmann::basic_json::binary 的用法
C++ JSON库 nlohmann::basic_json::binary 的用法
25 0
|
1月前
|
JSON 数据格式 C++
C++ JSON库 nlohmann::basic_json::begin() 的用法
C++ JSON库 nlohmann::basic_json::begin() 的用法
22 0