c++实现Xml和json互转【转】

时间:2023-03-09 15:19:16
c++实现Xml和json互转【转】

https://blog.****.net/kfy2011/article/details/51774242

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. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <time.h>
  9. #include <iostream>
  10. 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. while ((nPos = pNext.find("<")) >= 0)
  13. {
  14. // 获取第一个节点,如:<doc><a a1="1" a2="2">123</a></doc>
  15. int nPosS = pNext.find("<");
  16. int nPosE = pNext.find(">");
  17. if (nPosS < 0 || nPosE < 0)
  18. {
  19. printf("key error!");
  20. }
  21. string strKey = pNext.substr(nPosS+1, nPosE-nPosS-1);
  22. // 解释属性,如:<a a1="1" a2="2">
  23. cJSON * jsonVal = NULL;
  24. if ((nPos = strKey.find("=")) > 0)
  25. {
  26. jsonVal = cJSON_CreateObject();
  27. int nPos = strKey.find(" ");
  28. string temp = strKey.substr(nPos+1);
  29. strKey = strKey.substr(0, nPos);
  30. while((nPos = temp.find("=")) > 0)
  31. {
  32. int nPos1 = temp.find("=");
  33. int nPos2 = temp.find("\" ", nPos1 + 1);
  34. string strSubKey = temp.substr(0, nPos1);
  35. string strSubVal = temp.substr(nPos1+1);
  36. if (nPos2 > 0)
  37. strSubVal = temp.substr(nPos1+1, nPos2-nPos1-1);
  38. // 去除转义字符 \"
  39. if ((int)(nPos = strSubVal.find("\"")) >= 0)
  40. {
  41. int nEnd = strSubVal.find("\\", nPos+1);
  42. strSubVal = strSubVal.substr(nPos+1, nEnd-nPos-1);
  43. }
  44. cJSON_AddItemToObject(jsonVal, ("-" + strSubKey).c_str(), cJSON_CreateString(strSubVal.c_str()));
  45. if (nPos2 < 0)
  46. break;
  47. temp = temp.substr(nPos2+2);
  48. }
  49. }
  50. int nPosKeyE = pNext.find("</" + strKey + ">");
  51. if (nPosKeyE < 0)
  52. {
  53. printf("key error!");
  54. }
  55. // 获取节点内容,如<a a1="1" a2="2">123</a> 或 123
  56. string strVal = pNext.substr(nPosE+1, nPosKeyE-nPosE-1);
  57. if ((nPos = strVal.find("<")) >= 0)
  58. {
  59. // 包含子节点,如<a a1="1" a2="2">123</a>
  60. strVal = Xml2Json(strVal);
  61. if (jsonVal)
  62. {
  63. if (strVal != "")
  64. cJSON_AddItemToObject(jsonVal, "#text", cJSON_Parse(strVal.c_str()));
  65. }
  66. else
  67. {
  68. jsonVal = cJSON_Parse(strVal.c_str());
  69. }
  70. }
  71. else
  72. {
  73. // 不包含子节点,如123
  74. if (jsonVal)
  75. {
  76. if (strVal != "")
  77. cJSON_AddItemToObject(jsonVal, "#text", cJSON_CreateString(strVal.c_str()));
  78. }
  79. else
  80. {
  81. jsonVal = cJSON_CreateString(strVal.c_str());
  82. }
  83. }
  84. // 获取下一个节点
  85. pNext = pNext.substr(nPosKeyE + strKey.size() + 3);
  86. // 根据下一节点判断是否为数组
  87. int nPosNext = pNext.find("<");
  88. int nPosNextSame = pNext.find("<" + strKey + ">");
  89. if (strArrayKey != "" || (nPosNext>=0 && nPosNextSame>=0 && nPosNext==nPosNextSame))
  90. {
  91. // 数组
  92. cJSON_AddItemToArray(jsonArray, jsonVal);
  93. strArrayKey = strKey;
  94. }
  95. else
  96. {
  97. // 非数组
  98. cJSON_AddItemToObject(reJson, strKey.c_str(), jsonVal);
  99. }
  100. }
  101. if (strArrayKey != "")
  102. {
  103. cJSON_AddItemToObject(reJson, strArrayKey.c_str(), jsonArray);
  104. }
  105. string strJson = cJSON_Print(reJson);
  106. if(reJson)
  107. {
  108. cJSON_Delete(reJson);
  109. reJson = NULL;
  110. }
  111. return strJson;
  112. }

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. cJSON *pNext = root->child;
  15. if (!pNext)
  16. {
  17. return strJson;
  18. }
  19. int nPos = 0;
  20. while (pNext)
  21. {
  22. string strChild = cJSON_Print(pNext);
  23. string strVal = Json2Xml(strChild);
  24. if (pNext->string != NULL)
  25. {
  26. string strKey = pNext->string;
  27. if ((nPos=strKey.find("-")) == 0)
  28. {
  29. // 属性项
  30. strXml.append(" ");
  31. strXml.append(strKey.substr(1));
  32. strXml.append("=");
  33. strXml.append(strVal);
  34. if (pNext->next == NULL)
  35. strXml.append(">");
  36. }
  37. else if ((nPos=strKey.find("#")) == 0)
  38. {
  39. // 值
  40. strXml.append(">");
  41. strXml.append(strVal);
  42. }
  43. else if ((int)(strVal.find("=")) > 0 /*&& (int)(strVal.find("<")) < 0*/)
  44. {
  45. // 包含属性项的键值对
  46. strXml.append("<" + strKey);
  47. strXml.append(strVal);
  48. strXml.append("</" + strKey + ">");
  49. }
  50. else
  51. {
  52. // 修正底层无键的值数组的键,如:把<JUAN_XJ_preKey>123</JUAN_XJ_preKey>中的JUAN_XJ_preKey修正
  53. if ((int)strVal.find("JUAN_XJ_preKey") >= 0)
  54. {
  55. replace_all(strVal, "JUAN_XJ_preKey", strKey);
  56. strXml.append(strVal);
  57. }
  58. else
  59. {
  60. strXml.append("<" + strKey + ">");
  61. strXml.append(strVal);
  62. strXml.append("</" + strKey + ">");
  63. }
  64. }
  65. }
  66. else
  67. {
  68. // 不包含键的值数组, 如:["123", "456"],暂时转为<JUAN_XJ_preKey>123</JUAN_XJ_preKey>
  69. string strPreKey = "JUAN_XJ_preKey";
  70. strXml.append("<" + strPreKey + ">");
  71. strXml.append(strVal);
  72. strXml.append("</" + strPreKey + ">");
  73. }
  74. pNext = pNext->next;
  75. }
  76. return strXml;
  77. }

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