c++实现Xml和json互转【转】
https://blog.csdn.net/kfy2011/article/details/51774242
1、下载c语言的cJson库源码,库很小,只有两个文件cJSON.c和cJSON.h。下载地址:https://sourceforge.net/projects/cjson/
2、c++实现Xml和json互转
2.1、头文件
- #include "XmlJsonTransfer.h"
- //#include "cmsDebug.h"
- #include "WebSocket/src/cJSON.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
- #include <time.h>
- #include <iostream>
- using namespace std;
2.2、xml转json
- // 依赖cJSon,递归
- /*
- <doc><a a1="1" a2="2">123</a></doc> 转 {"doc": {"a": {"-a1": "1","-a2": "2","#text": "123"}}}
- */
- string Xml2Json(string strXml)
- {
- string pNext = strXml;
- cJSON * reJson = cJSON_CreateObject();
- cJSON * jsonArray = cJSON_CreateArray();
- string strArrayKey = "";
- int nPos = 0;
- while ((nPos = pNext.find("<")) >= 0)
- {
- // 获取第一个节点,如:<doc><a a1="1" a2="2">123</a></doc>
- int nPosS = pNext.find("<");
- int nPosE = pNext.find(">");
- if (nPosS < 0 || nPosE < 0)
- {
- printf("key error!");
- }
- string strKey = pNext.substr(nPosS+1, nPosE-nPosS-1);
- // 解释属性,如:<a a1="1" a2="2">
- cJSON * jsonVal = NULL;
- if ((nPos = strKey.find("=")) > 0)
- {
- jsonVal = cJSON_CreateObject();
- int nPos = strKey.find(" ");
- string temp = strKey.substr(nPos+1);
- strKey = strKey.substr(0, nPos);
- while((nPos = temp.find("=")) > 0)
- {
- int nPos1 = temp.find("=");
- int nPos2 = temp.find("\" ", nPos1 + 1);
- string strSubKey = temp.substr(0, nPos1);
- string strSubVal = temp.substr(nPos1+1);
- if (nPos2 > 0)
- strSubVal = temp.substr(nPos1+1, nPos2-nPos1-1);
- // 去除转义字符 \"
- if ((int)(nPos = strSubVal.find("\"")) >= 0)
- {
- int nEnd = strSubVal.find("\\", nPos+1);
- strSubVal = strSubVal.substr(nPos+1, nEnd-nPos-1);
- }
- cJSON_AddItemToObject(jsonVal, ("-" + strSubKey).c_str(), cJSON_CreateString(strSubVal.c_str()));
- if (nPos2 < 0)
- break;
- temp = temp.substr(nPos2+2);
- }
- }
- int nPosKeyE = pNext.find("</" + strKey + ">");
- if (nPosKeyE < 0)
- {
- printf("key error!");
- }
- // 获取节点内容,如<a a1="1" a2="2">123</a> 或 123
- string strVal = pNext.substr(nPosE+1, nPosKeyE-nPosE-1);
- if ((nPos = strVal.find("<")) >= 0)
- {
- // 包含子节点,如<a a1="1" a2="2">123</a>
- strVal = Xml2Json(strVal);
- if (jsonVal)
- {
- if (strVal != "")
- cJSON_AddItemToObject(jsonVal, "#text", cJSON_Parse(strVal.c_str()));
- }
- else
- {
- jsonVal = cJSON_Parse(strVal.c_str());
- }
- }
- else
- {
- // 不包含子节点,如123
- if (jsonVal)
- {
- if (strVal != "")
- cJSON_AddItemToObject(jsonVal, "#text", cJSON_CreateString(strVal.c_str()));
- }
- else
- {
- jsonVal = cJSON_CreateString(strVal.c_str());
- }
- }
- // 获取下一个节点
- pNext = pNext.substr(nPosKeyE + strKey.size() + 3);
- // 根据下一节点判断是否为数组
- int nPosNext = pNext.find("<");
- int nPosNextSame = pNext.find("<" + strKey + ">");
- if (strArrayKey != "" || (nPosNext>=0 && nPosNextSame>=0 && nPosNext==nPosNextSame))
- {
- // 数组
- cJSON_AddItemToArray(jsonArray, jsonVal);
- strArrayKey = strKey;
- }
- else
- {
- // 非数组
- cJSON_AddItemToObject(reJson, strKey.c_str(), jsonVal);
- }
- }
- if (strArrayKey != "")
- {
- cJSON_AddItemToObject(reJson, strArrayKey.c_str(), jsonArray);
- }
- string strJson = cJSON_Print(reJson);
- if(reJson)
- {
- cJSON_Delete(reJson);
- reJson = NULL;
- }
- return strJson;
- }
2.3、json转xml
- // 依赖cJSon,递归
- /*
- {"doc": {"a": {"-a1": "1","-a2": "2","#text": "123"}}} 转 <doc><a a1="1" a2="2">123</a></doc>
- */
- string Json2Xml(string strJson)
- {
- string strXml = "";
- cJSON *root = cJSON_Parse(strJson.c_str());
- if (!root)
- {
- printf("strJson error!");
- return "";
- }
- cJSON *pNext = root->child;
- if (!pNext)
- {
- return strJson;
- }
- int nPos = 0;
- while (pNext)
- {
- string strChild = cJSON_Print(pNext);
- string strVal = Json2Xml(strChild);
- if (pNext->string != NULL)
- {
- string strKey = pNext->string;
- if ((nPos=strKey.find("-")) == 0)
- {
- // 属性项
- strXml.append(" ");
- strXml.append(strKey.substr(1));
- strXml.append("=");
- strXml.append(strVal);
- if (pNext->next == NULL)
- strXml.append(">");
- }
- else if ((nPos=strKey.find("#")) == 0)
- {
- // 值
- strXml.append(">");
- strXml.append(strVal);
- }
- else if ((int)(strVal.find("=")) > 0 /*&& (int)(strVal.find("<")) < 0*/)
- {
- // 包含属性项的键值对
- strXml.append("<" + strKey);
- strXml.append(strVal);
- strXml.append("</" + strKey + ">");
- }
- else
- {
- // 修正底层无键的值数组的键,如:把<JUAN_XJ_preKey>123</JUAN_XJ_preKey>中的JUAN_XJ_preKey修正
- if ((int)strVal.find("JUAN_XJ_preKey") >= 0)
- {
- replace_all(strVal, "JUAN_XJ_preKey", strKey);
- strXml.append(strVal);
- }
- else
- {
- strXml.append("<" + strKey + ">");
- strXml.append(strVal);
- strXml.append("</" + strKey + ">");
- }
- }
- }
- else
- {
- // 不包含键的值数组, 如:["123", "456"],暂时转为<JUAN_XJ_preKey>123</JUAN_XJ_preKey>
- string strPreKey = "JUAN_XJ_preKey";
- strXml.append("<" + strPreKey + ">");
- strXml.append(strVal);
- strXml.append("</" + strPreKey + ">");
- }
- pNext = pNext->next;
- }
- return strXml;
- }
2.4、辅助函数
- // 替换字符串
- string& replace_all(string& str, const string& old_value, const string& new_value)
- {
- while(true)
- {
- string::size_type pos(0);
- if((pos=str.find(old_value)) != string::npos)
- str.replace(pos,old_value.length(),new_value);
- else
- break;
- }
- return str;
- }
c++实现Xml和json互转【转】的更多相关文章
- JavaScript实现XML与JSON互转代码(转载)
下面来分享一个关于JavaScript实现XML与JSON互转例子,这里面介绍了国外的三款xml转json的例子,希望这些例子能给你带来帮助. 最近在开发在线XML编辑器,打算使用JSON做为中间格式 ...
- SQL2008使用json.net实现XML与JSON互转
借助CLR,首先实现字符串的互转,然后使用存储过程实现JSON2table public class JsonFunction { /// <summary> ...
- JSONUtil(JAVA对象/List与json互转,xml与json互转)
package com.chauvet.utils.json; import java.io.BufferedReader; import java.io.File; import java.io.F ...
- xml与json互转
依赖包: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib< ...
- C# :XML和JSON互转
我们一般在用JSON或者XML作为数据交换的时候,可能定义一个没有真正意义方法的类,其实就是一个关于属性的数据结构,如果对于这种情况,可以将这个类对象作为中介,然后利用C#提供的序列化和反序列化的方法 ...
- xml和json互转
开发过程中有些时候需要把xml和json互转,如某钱X接口入参和出参都是xml格式的,十分蛋疼.特写下面工具类,以留用. 需要引用jar: <!-- https://mvnrepository. ...
- Json、JavaBean、Map、XML之间的互转
思路是JavaBean.Map.XML都可以用工具类很简单的转换为Json,进而实现互相转换 1.Map.XML与Json互转 mvn依赖 <dependency> <groupId ...
- c#通用配置文件读写类(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- c#通用配置文件读写类与格式转换(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
随机推荐
- Linux-C基础编程
GCC工作流程 工作流程 1.预处理 -E xxx.c —> xxx.i 宏替换:头文件展开:注释去掉: gcc -E hello.c -o hello.i 2.编译 -S xxx.i —> ...
- BZOJ 4636: 蒟蒻的数列 分块
4636: 蒟蒻的数列 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4636 Description 蒟蒻DCrusher不仅喜欢玩扑克 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem F. Judging Time Prediction 优先队列
Problem F. Judging Time Prediction 题目连接: http://www.codeforces.com/gym/100253 Description It is not ...
- delete void pointer
delete void pointer是否会有内存泄漏? 看下面一个简单例子 class Test{ public: Test(){ printf ("constructor\n" ...
- Java 与 .NET 的平台发展之争
Java 8即将正式发布,从早期版本中,我们已经可以领略到一些令人兴奋的特性.但是开发者Andrew C. Oliver表示,尽管如此,Java语言在某些特性上还是落后于.Net.比如,Java 8中 ...
- gitblit.cmd运行自动关闭
前几天运行gitblit.cmd一直正常,今天运行gitblit.cmd,几秒钟后命令行窗口就自动关闭了,导致无法启动gitblit服务器,查看日志如下: 刚开始以为是防火墙问题,在防火墙中添加了程序 ...
- 【权限设计】一个案例,三个角色,简单说下B端产品的权限设计
入行以来也接触过一些B端产品,这些产品之中权限管理是重中之重,权限管理不仅仅是整个系统的一个小小的模块,它一直贯穿整个系统,从登陆到操作到最后的登出.说它相当的复杂真不为过. 对于权限,如果从控制力来 ...
- phonegap helloworld 之android
一 phonegap cordova: http://cordova.apache.org/phonegap: http://phonegap.com PhoneGap 是Cordova的一个开源的发 ...
- SQLPrompt_7.2.2.273〖含注册机〗(支持低版本和最高版本SQL2016+VS2015)
SQLPrompt_7.4.1.564[含注册机](支持低版本和最高版本SQL2016+VS2015) http://download.csdn.net/detail/wozengcong/97601 ...
- 警告 7 隐藏了继承的成员。如果是有意隐藏,请使用关键字 new
public new bool Print(string 承包方编码, MapPrint.My2Progress pMy2Progress, bool Label2ZJ)