c++实现Xml和json互转

1、下载c语言的cJson库源码,库很小,只有两个文件cJSON.c和cJSON.h。下载地址:https://sourceforge.net/projects/cjson/

2、c++实现Xml和json互转

2.1、头文件

 
  1. #include "XmlJsonTransfer.h"

  2. //#include "cmsDebug.h"

  3. #include "WebSocket/src/cJSON.h"

  4.  
  5. #include <stdlib.h>

  6. #include <string.h>

  7. #include <stdio.h>

  8. #include <assert.h>

  9. #include <time.h>

  10. #include <iostream>

  11. using namespace std;


2.2、xml转json

 
  1. // 依赖cJSon,递归

  2. /*

  3. <doc><a a1="1" a2="2">123</a></doc> 转 {"doc": {"a": {"-a1": "1","-a2": "2","#text": "123"}}}

  4. */

  5. string Xml2Json(string strXml)

  6. {

  7. string pNext = strXml;

  8. cJSON * reJson = cJSON_CreateObject();

  9. cJSON * jsonArray = cJSON_CreateArray();

  10. string strArrayKey = "";

  11. int nPos = 0;

  12.  
  13. while ((nPos = pNext.find("<")) >= 0)

  14. {

  15. // 获取第一个节点,如:<doc><a a1="1" a2="2">123</a></doc>

  16. int nPosS = pNext.find("<");

  17. int nPosE = pNext.find(">");

  18. if (nPosS < 0 || nPosE < 0)

  19. {

  20. printf("key error!");

  21. }

  22.  
  23. string strKey = pNext.substr(nPosS+1, nPosE-nPosS-1);

  24. // 解释属性,如:<a a1="1" a2="2">

  25. cJSON * jsonVal = NULL;

  26. if ((nPos = strKey.find("=")) > 0)

  27. {

  28. jsonVal = cJSON_CreateObject();

  29. int nPos = strKey.find(" ");

  30. string temp = strKey.substr(nPos+1);

  31. strKey = strKey.substr(0, nPos);

  32. while((nPos = temp.find("=")) > 0)

  33. {

  34. int nPos1 = temp.find("=");

  35. int nPos2 = temp.find("\" ", nPos1 + 1);

  36.  
  37. string strSubKey = temp.substr(0, nPos1);

  38. string strSubVal = temp.substr(nPos1+1);

  39. if (nPos2 > 0)

  40. strSubVal = temp.substr(nPos1+1, nPos2-nPos1-1);

  41.  
  42. // 去除转义字符 \"

  43. if ((int)(nPos = strSubVal.find("\"")) >= 0)

  44. {

  45. int nEnd = strSubVal.find("\\", nPos+1);

  46. strSubVal = strSubVal.substr(nPos+1, nEnd-nPos-1);

  47. }

  48. cJSON_AddItemToObject(jsonVal, ("-" + strSubKey).c_str(), cJSON_CreateString(strSubVal.c_str()));

  49.  
  50. if (nPos2 < 0)

  51. break;

  52.  
  53. temp = temp.substr(nPos2+2);

  54. }

  55. }

  56.  
  57. int nPosKeyE = pNext.find("</" + strKey + ">");

  58. if (nPosKeyE < 0)

  59. {

  60. printf("key error!");

  61. }

  62. // 获取节点内容,如<a a1="1" a2="2">123</a> 或 123

  63. string strVal = pNext.substr(nPosE+1, nPosKeyE-nPosE-1);

  64. if ((nPos = strVal.find("<")) >= 0)

  65. {

  66. // 包含子节点,如<a a1="1" a2="2">123</a>

  67. strVal = Xml2Json(strVal);

  68.  
  69. if (jsonVal)

  70. {

  71. if (strVal != "")

  72. cJSON_AddItemToObject(jsonVal, "#text", cJSON_Parse(strVal.c_str()));

  73. }

  74. else

  75. {

  76. jsonVal = cJSON_Parse(strVal.c_str());

  77. }

  78. }

  79. else

  80. {

  81. // 不包含子节点,如123

  82. if (jsonVal)

  83. {

  84. if (strVal != "")

  85. cJSON_AddItemToObject(jsonVal, "#text", cJSON_CreateString(strVal.c_str()));

  86. }

  87. else

  88. {

  89. jsonVal = cJSON_CreateString(strVal.c_str());

  90. }

  91. }

  92.  
  93. // 获取下一个节点

  94. pNext = pNext.substr(nPosKeyE + strKey.size() + 3);

  95.  
  96. // 根据下一节点判断是否为数组

  97. int nPosNext = pNext.find("<");

  98. int nPosNextSame = pNext.find("<" + strKey + ">");

  99. if (strArrayKey != "" || (nPosNext>=0 && nPosNextSame>=0 && nPosNext==nPosNextSame))

  100. {

  101. // 数组

  102. cJSON_AddItemToArray(jsonArray, jsonVal);

  103. strArrayKey = strKey;

  104. }

  105. else

  106. {

  107. // 非数组

  108. cJSON_AddItemToObject(reJson, strKey.c_str(), jsonVal);

  109. }

  110. }

  111.  
  112.  
  113. if (strArrayKey != "")

  114. {

  115. cJSON_AddItemToObject(reJson, strArrayKey.c_str(), jsonArray);

  116. }

  117.  
  118. string strJson = cJSON_Print(reJson);

  119.  
  120. if(reJson)

  121. {

  122. cJSON_Delete(reJson);

  123. reJson = NULL;

  124. }

  125.  
  126. return strJson;

  127. }


2.3、json转xml

 
  1. // 依赖cJSon,递归

  2. /*

  3. {"doc": {"a": {"-a1": "1","-a2": "2","#text": "123"}}} 转 <doc><a a1="1" a2="2">123</a></doc>

  4. */

  5. string Json2Xml(string strJson)

  6. {

  7. string strXml = "";

  8. cJSON *root = cJSON_Parse(strJson.c_str());

  9. if (!root)

  10. {

  11. printf("strJson error!");

  12. return "";

  13. }

  14.  
  15. cJSON *pNext = root->child;

  16. if (!pNext)

  17. {

  18. return strJson;

  19. }

  20.  
  21. int nPos = 0;

  22. while (pNext)

  23. {

  24. string strChild = cJSON_Print(pNext);

  25. string strVal = Json2Xml(strChild);

  26.  
  27. if (pNext->string != NULL)

  28. {

  29. string strKey = pNext->string;

  30. if ((nPos=strKey.find("-")) == 0)

  31. {

  32. // 属性项

  33. strXml.append(" ");

  34. strXml.append(strKey.substr(1));

  35. strXml.append("=");

  36. strXml.append(strVal);

  37.  
  38. if (pNext->next == NULL)

  39. strXml.append(">");

  40. }

  41. else if ((nPos=strKey.find("#")) == 0)

  42. {

  43. // 值

  44. strXml.append(">");

  45. strXml.append(strVal);

  46. }

  47. else if ((int)(strVal.find("=")) > 0 /*&& (int)(strVal.find("<")) < 0*/)

  48. {

  49. // 包含属性项的键值对

  50. strXml.append("<" + strKey);

  51. strXml.append(strVal);

  52. strXml.append("</" + strKey + ">");

  53. }

  54. else

  55. {

  56. // 修正底层无键的值数组的键,如:把<JUAN_XJ_preKey>123</JUAN_XJ_preKey>中的JUAN_XJ_preKey修正

  57. if ((int)strVal.find("JUAN_XJ_preKey") >= 0)

  58. {

  59. replace_all(strVal, "JUAN_XJ_preKey", strKey);

  60. strXml.append(strVal);

  61. }

  62. else

  63. {

  64. strXml.append("<" + strKey + ">");

  65. strXml.append(strVal);

  66. strXml.append("</" + strKey + ">");

  67. }

  68. }

  69. }

  70. else

  71. {

  72. // 不包含键的值数组, 如:["123", "456"],暂时转为<JUAN_XJ_preKey>123</JUAN_XJ_preKey>

  73. string strPreKey = "JUAN_XJ_preKey";

  74. strXml.append("<" + strPreKey + ">");

  75. strXml.append(strVal);

  76. strXml.append("</" + strPreKey + ">");

  77. }

  78.  
  79. pNext = pNext->next;

  80. }

  81.  
  82. return strXml;

  83. }

2.4、辅助函数

 
  1. // 替换字符串

  2. string& replace_all(string& str, const string& old_value, const string& new_value)

  3. {

  4. while(true)

  5. {

  6. string::size_type pos(0);

  7. if((pos=str.find(old_value)) != string::npos)

  8. str.replace(pos,old_value.length(),new_value);

  9. else

  10. break;

  11. }

  12. return str;

  13. }

猜你喜欢

转载自blog.csdn.net/Pei_hua100/article/details/81279268