欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++常用函數(shù)之XML JSON格式轉(zhuǎn)換問題

 更新時(shí)間:2020年02月29日 21:04:50   作者:程序員的成長(zhǎng)  
XML在Json出現(xiàn)前應(yīng)用很廣泛,靈活性好,應(yīng)用語(yǔ)言也沒有限制,發(fā)展了這么長(zhǎng)時(shí)間后xml標(biāo)準(zhǔn)已經(jīng)很臃腫。這篇文章主要介紹了C++常用函數(shù)之XML JSON格式轉(zhuǎn)換問題,需要的朋友可以參考下

數(shù)據(jù)格式在編程里面很常見,不同的系統(tǒng)都會(huì)有自己的標(biāo)準(zhǔn)。因?yàn)榻o有各的定義,每次做第三方開發(fā)系統(tǒng)對(duì)接的時(shí)候數(shù)據(jù)格式標(biāo)準(zhǔn)都是頭疼的事情。

在開發(fā)過程中比較常見的比如有Json、XML、Key-Value等。這里我們就先看看Json和XML。兩者的轉(zhuǎn)換有很多開源的代碼可以使用,而且也很完善,可以參考xml2json、xsltjson。

XML在Json出現(xiàn)前應(yīng)用很廣泛,靈活性好,應(yīng)用語(yǔ)言也沒有限制,發(fā)展了這么長(zhǎng)時(shí)間后xml標(biāo)準(zhǔn)已經(jīng)很臃腫。這里可以查看XML的標(biāo)準(zhǔn)XML標(biāo)準(zhǔn)。在C++里面解析和操作XML的庫(kù)也有不少,tinyxml 就是個(gè)不錯(cuò)的選擇,體積少、簡(jiǎn)單、高效的開源庫(kù),現(xiàn)在已經(jīng)發(fā)布了TinyXml-2.

Json出來(lái)后立即被很多高級(jí)語(yǔ)言作為了標(biāo)準(zhǔn)推薦使用,如果想了解Json的定義請(qǐng)點(diǎn)擊這里:JSON定義

XML2Json & Json2XML

接下來(lái),我想做個(gè)簡(jiǎn)單的函數(shù)來(lái)轉(zhuǎn)換。

<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>

上面的報(bào)文是在三方支付里面常見的報(bào)文,這次我們來(lái)實(shí)現(xiàn)對(duì)這段報(bào)文的Json格式的自由轉(zhuǎn)換。

#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 做轉(zhuǎn)換,需要將兩者的頭文件和源代碼文件下載,并在編譯中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++常用函數(shù) XML JSON格式轉(zhuǎn)換https://www.cppentry.com/benc...

開發(fā)資料https://www.cppentry.com

知識(shí)點(diǎn)擴(kuò)展:C++常用的系統(tǒng)函數(shù)

數(shù)學(xué)<math.h>:

1 三角函數(shù)

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

2 反三角函數(shù)

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

3 雙曲三角函數(shù)

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

4 指數(shù)與對(duì)數(shù)

double exp (double x); e的x次冪
double pow (double x, double y); x的y次冪
double sqrt (double);
double log (double x); 以e為底的對(duì)數(shù),即ln x
double log10 (double x);log10(x) 以10為底。

沒有以2為底的函數(shù)但是可以用換底公式解 決:log2(N)=log10(N)/log10(2)

5 取整

double ceil (double); 不小于x的最小整數(shù)
double floor (double); 不大于x的最大整數(shù)

6 絕對(duì)值

int abs(int);整型
long labs(long);長(zhǎng)整型
double fabs (double);浮點(diǎn)型

7 標(biāo)準(zhǔn)化浮點(diǎn)數(shù)

double frexp (double f, int p); 標(biāo)準(zhǔn)化浮點(diǎn)數(shù), 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*); 將參數(shù)的整數(shù)部分通過指針回傳, 返回小數(shù)部分
double fmod (double, double); 返回兩參數(shù)相除的余數(shù)

9.平方根

double sqrt(double x);

字符<ctype.h>:

int isalpha(int c);c是否為字母
int isdigit(int c);c是否為數(shù)字
int tolower(int c);將c轉(zhuǎn)換為小寫字母
int toupper(int c);將c轉(zhuǎn)換為大寫字母

字符串<string.h>:

char strcpy(char sl,char s2);將字符串s2復(fù)制給s1,即覆蓋
unsigned strlen(char sr);求字符串str長(zhǎng)度

內(nèi)存操作<memory.h>:

void memcpy(void d,void *s,int c);將s指向的內(nèi)存區(qū)域的c個(gè)字節(jié)復(fù)制到d指向的區(qū)域

類型轉(zhuǎn)換<stdlib.h>:

int atoi(char s);將字符串轉(zhuǎn)化為整數(shù)
char itoa(int v,char *s,int x);將整數(shù)v按x進(jìn)制轉(zhuǎn)成字符串s

時(shí)間<time.h>:

time_t time(time_t *timer);返回1970/1/1零點(diǎn)到目前的秒數(shù)

其他<stdlib.h>:

srand(unsigned seed);設(shè)置隨機(jī)數(shù)的種子
int rand();產(chǎn)生0-RAND_MAX的隨機(jī)數(shù)
exit(int);終止正在執(zhí)行的程序

keep going

到此這篇關(guān)于C++常用函數(shù)之XML JSON格式轉(zhuǎn)換問題的文章就介紹到這了,更多相關(guān)c++ xml json格式轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論