怎么在C++中对XML与JSON格式进行转换

发布时间:2021-03-17 14:33:42 作者:Leah
来源:亿速云 阅读:602

怎么在C++中对XML与JSON格式进行转换?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<xml>
  <appid>appid-value111111</appid>
  <mch_id>mch_id-value22222</mch_id>
  <nonce_str>nonce_str-value3333333</nonce_str>
  <transaction_id>transaction_id-value44444444</transaction_id>
  <sign>sign-value5555555555</sign>
</xml>

上面的报文是在三方支付里面常见的报文,这次我们来实现对这段报文的Json格式的自由转换。

#include <string>
#include <iostream>
#include "tinyxml2.h"
#include "nlohmann/json.hpp"

using json = nlohmann::json;
using namespace tinyxml2;
using namespace std;

string xml2json(string &src)
{
  XMLDocument doc;
  doc.Parse( src.c_str() );
  
  json root;
  XMLElement* rootElement = doc.RootElement();
  XMLElement* child = rootElement->FirstChildElement();
  while(child) {
    const char* title = child->Name() ;
    const char* value = child->GetText();
    child = child->NextSiblingElement();
    root[title]=value ;
  }
  return root.dump() ;
}

string json2xml(string& src)
{
  XMLDocument xmlDoc;
  XMLNode * pRoot = xmlDoc.NewElement("xml");
  xmlDoc.InsertFirstChild(pRoot);
  auto j3 = json::parse(src.c_str());
  for (json::iterator it = j3.begin(); it != j3.end(); ++it) {
    string key = it.key();
    string value = it.value() ;
    XMLElement * pElement = xmlDoc.NewElement(key.c_str()) ;
    pElement->SetText(value.c_str()) ;
    pRoot->InsertEndChild(pElement);
  }
  XMLPrinter printer;
  pRoot->Accept( &printer );
  return printer.CStr();
}

int main()
{
  string src = "<xml>\
          <appid>appid-value111111</appid>\
          <mch_id>mch_id-value22222</mch_id>\
          <nonce_str>nonce_str-value3333333</nonce_str>\
          <transaction_id>transaction_id-value44444444</transaction_id>\
          <sign>sign-value5555555555</sign>\
        </xml>" ;
  string json = xml2json(src) ;
  string xml = json2xml(json) ;

  cout << json ;
  cout << endl ;
  cout << xml ;
}

这次我们使用tinyxml2 和nlohmann json 做转换,需要将两者的头文件和源代码文件下载,并在编译中include。

nolhmann json 需要C++ 11 的支持,gcc版本需要在4.7以上。

可以使用下面命令编译:

g++ -std=c++11 xmljson.cpp tinyxml2.cpp -I./

./a.out
{"appid":"appid-value111111","mch_id":"mch_id-value22222","nonce_str":"nonce_str-value3333333","sign":"sign-value5555555555","transaction_id":"transaction_id-value44444444"}
<xml>
  <appid>appid-value111111</appid>
  <mch_id>mch_id-value22222</mch_id>
  <nonce_str>nonce_str-value3333333</nonce_str>
  <sign>sign-value5555555555</sign>
  <transaction_id>transaction_id-value44444444</transaction_id>
</xml>

C++常用函数 XML JSON格式转换https://www.cppentry.com/benc...

开发资料https://www.cppentry.com

知识点扩展:C++常用的系统函数

数学<math.h>:

1 三角函数

double sin (double);
double cos (double);
double tan (double);

2 反三角函数

double asin (double); 结果介于[-PI/2, PI/2]
double acos (double); 结果介于[0, PI]
double atan (double); 反正切(主值), 结果介于[-PI/2, PI/2]
double atan2 (double, double); 反正切(整圆值), 结果介于[-PI/2, PI/2]

3 双曲三角函数

double sinh (double);
double cosh (double);
double tanh (double);

4 指数与对数

double exp (double x); e的x次幂
double pow (double x, double y); x的y次幂
double sqrt (double);
double log (double x); 以e为底的对数,即ln x
double log10 (double x);log10(x) 以10为底。

没有以2为底的函数但是可以用换底公式解 决:log2(N)=log10(N)/log10(2)

5 取整

double ceil (double); 不小于x的最小整数
double floor (double); 不大于x的最大整数

6 绝对值

int abs(int);整型
long labs(long);长整型
double fabs (double);浮点型

7 标准化浮点数

double frexp (double f, int p); 标准化浮点数, f = x 2^p, 已知f求x, p ( x介于[0.5, 1] )
double ldexp (double x, int p); 与frexp相反, 已知x, p求f

8 取整与取余

double modf (double, double*); 将参数的整数部分通过指针回传, 返回小数部分
double fmod (double, double); 返回两参数相除的余数

9.平方根

double sqrt(double x);

字符<ctype.h>:

int isalpha(int c);c是否为字母
int isdigit(int c);c是否为数字
int tolower(int c);将c转换为小写字母
int toupper(int c);将c转换为大写字母

字符串<string.h>:

char strcpy(char sl,char s2);将字符串s2复制给s1,即覆盖
unsigned strlen(char sr);求字符串str长度

内存操作<memory.h>:

void memcpy(void d,void *s,int c);将s指向的内存区域的c个字节复制到d指向的区域

类型转换<stdlib.h>:

int atoi(char s);将字符串转化为整数
char itoa(int v,char *s,int x);将整数v按x进制转成字符串s

时间<time.h>:

time_t time(time_t *timer);返回1970/1/1零点到目前的秒数

其他<stdlib.h>:

srand(unsigned seed);设置随机数的种子
int rand();产生0-RAND_MAX的随机数
exit(int);终止正在执行的程序

keep going

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 怎么在Java中将Bean与xml进行转换
  2. 在java中怎么对字符进行全角与半角的转换

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ xml json

上一篇:CSS3中box-shadow属性怎么用

下一篇:css3如何实现小箭头各种图形效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》