开源资源库

jsoncpp-src-0.5.0.tar.gz:
https://sourceforge.net/projects/jsoncpp/

jsoncpp-master.zip
https://github.com/open-source-parsers/jsoncpp

下面以jsoncpp050版本为例

1:map转化为jsonstr

#include "json.h"
string map2jsonstr(const map<string,string>& map_info)
{
Json::Value jObject;
for (map<string, string>::const_iterator iter = map_info.begin( ); iter != map_info.end( ); ++iter)
{
jObject[iter->first] = iter->second;
}
return jObject.toStyledString();
}

2:jsonstr转化为map

string itoa_self(int i)
{
stringstream ss;
ss << i;
return ss.str();
} map<string,string> jsonstr2map(const string& json)
{
Json::Reader reader;
Json::Value value;
map<string, string> maps; if (json.length() > 0)
{
if (reader.parse(json, value))
{
Json::Value::Members members = value.getMemberNames();
for (Json::Value::Members::iterator it = members.begin(); it != members.end(); it++)
{
Json::ValueType vt = value[*it].type();
switch (vt)
{
case Json::stringValue:
{
maps.insert(pair<string, string>(*it, value[*it].asString()));
break;
}
case Json::intValue:
{
int intTmp = value[*it].asInt();
maps.insert(pair<string, string>(*it, itoa_self(intTmp)));
break;
}
case Json::arrayValue:
{
std::string strid;
for (unsigned int i = 0; i < value[*it].size(); i++)
{
strid +=value[*it][i].asString();
strid +=",";
}
if(!strid.empty())
{
strid = strid.substr(0,strid.size()-1);
}
maps.insert(pair<string, string>(*it, strid));
break;
}
default:
{
break;
}
}//end switch
}//end for
}//end if
} return maps;
}

  

C++实现json字符串与map的转换的更多相关文章

  1. json字符串转map

    <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...

  2. JSON字符串与Map互转

    //一.map转为json字符串 public static String map2jsonstr(Map<String,?> map){ return JSONObject.toJSON ...

  3. Java基础97 json插件的使用(java对象和json字符串对象之间的转换)

    1.需要用到的包 2.实例 实体类 people package com.shore.entity; /** * @author DSHORE/2019-4-19 * */ public class ...

  4. json字符串转map、json数组演示

    公司项目用的IBM封装的json解析,此处采用阿里的fastjson进行演示,代码如下: package com.alphajuns.test; import com.alibaba.fastjson ...

  5. [转]Json字符串和map和HashMap之间的转换

    需要导入alibaba.fastJsonmaven中的依赖为 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> ...

  6. Json字符串转map集合

    第一步:在pom.xml中添加依赖; <dependency> <groupId>com.alibaba</groupId> <artifactId>f ...

  7. JSON字符串转换为Map

    本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...

  8. JSON字符串和对象 的转换

    一  通过eval() 函数可以将JSON字符串转化为对象 var obj = eval('(' + str + ')'); 或者 var obj = str.parseJSON(); //由JSON ...

  9. 前端页面使用 Json对象与Json字符串之间的互相转换

    前言 在前端页面很多时候都会用到Json这种格式的数据,最近没有前端,后端的我也要什么都要搞,对于Json对象与Json字符串之间的转换终于摸清楚了几种方式,归纳如下! 一:Json对象转换为json ...

随机推荐

  1. $.on()方法和addEventListener改变this指向

    jQuery $.on()方法和addEventListener改变this指向 标签(空格分隔): jQuery JavaScript jQuery $.on() jq的绑定事件使用$([selec ...

  2. July 08th 2017 Week 27th Saturday

    You are never wrong to do the right thing. 坚持做对的事情,永远都不会错. I think the translation may be not precis ...

  3. July 02nd 2017 Week 27th Sunday

    No safe wading in an unknown water. 未知水深浅,涉水有危险. Is this the theory that has been the guideline for ...

  4. Python切片(入门7)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407977.html 本文出自:[Edwin博客园] Python切片 1. 对list进行切片 L = r ...

  5. dynamic_cast动态转换

    我们都知道dynamic_cast会在运行时进行类型检查,比较安全,static_cast静态转换,不安全 dynamic_cast转换的类型必须是个类,且这个类中必须有虚函数,为什么呢? 虚函数对于 ...

  6. Unix I/O--输入/输出(I/O) : 是指主存和外部设备(如磁盘,终端,网络)之间拷贝数据过程

    输入/输出(I/O) : 是指主存和外部设备(如磁盘,终端,网络)之间拷贝数据过程 https://www.bbsmax.com/A/o75N88ZxzW/ 10.1 Unix I/O 一个Unix ...

  7. 4springboot:日志(上)

    1.主流的日志框架 2.SLF4J使用 如何在系统中使用SLF4j https://www.slf4j.org 以后开发的时候,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象层里面 ...

  8. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  9. LSOF 安装与使用(功能强大)

    Linux上安装: tar zxvf lsof_4.76.tar.gz cd lsof_4.76 ls 00.README.FIRST_4.76       lsof_4.76_src.tar.gz  ...

  10. zabbix-agent安装

    1.下载yum源库 rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm ...