LN : JSON (利用C++实现JSON)
- Appreciation to our TA, 王毅峰, who designed this task.
问题描述
JSON, JavaScript Object Notation,is an flexible format that uses human-readable text to transmit data objects consisting of key-value pairs(键值对)
To construct a json object, we need to parse a raw string
For example
// {"name":"lilei","country":"china","age":"20"}
// in constructor, we parse the string to map
// that is, we find the first key "name", and correspoding value "lilei"
// then we modify our private data member map<string, string> _data
// _data["name"] = "lilei"
// don't stop until all the key-value pairs are stored in _data
json test("{\"name\":\"lilei\",\"country\":\"china\",\"age\":\"20\"}");
NOTE:
To simplify the problem
- You just need to finish the constructor,which find out the key/value pairs and store in _data
- all the string doesn't consist of space(空格), and it is strictly formed like {"key1":"value1","key2":"value2","key3":"value3"}
- all the key and value have double quotation marks(双引号)
- in front of them and after them(所有键的前后和值的前后都有双引号)
- read json.h and main.cpp for more details
问题解析
问题的关键是如何从一个长字符串中获取对应的键值对,并且运用make_pair组成一组map。
json.h
#ifndef JSON_H
#define JSON_H
#include <iostream>
#include <string>
#include <map>
using std::ostream;
using std::string;
using std::map;
class json {
private:
// store the relationship between key and value
map<string, string> _data;
public:
// parse the raw string to map<string, string>
explicit json(string);
// return mutable value according to key
string& operator[](string key) {
return _data[key];
}
// return the number of key/value
int count() const {
return _data.size();
}
// output
friend ostream& operator<<(ostream& os, const json& obj) {
map<string, string>::iterator it;
map<string, string> data = obj._data;
int num = 0;
os << "{\n";
for (it = data.begin(); it != data.end(); it++) {
num++;
os << " \"" << it->first << "\": \"" << it->second << "\"";
if (num != obj.count()) {
os << ",";
}
os << "\n";
}
os << "}";
return os;
}
};
#endif // JSON_H
json.cpp
#include "json.h"
using namespace std;
json::json(string a) {
int len = a.length();
string m, n;
int famen = 0;
for (int i = 0; i < len; i++) {
if (a[i] == '"') {
famen++;
continue;
}
if (famen%4 == 1) {
m.push_back(a[i]);
} else if (famen%4 == 3) {
n.push_back(a[i]);
} else if (famen%4 == 0 && famen != 0) {
_data.insert(make_pair(m, n));
m.clear();
n.clear();
}
}
}
main.cpp
#include <iostream>
#include <string>
#include "json.h"
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main(void) {
{
// {"name":"lilei","country":"china","age":"20"}
json test("{\"name\":\"lilei\",\"country\":\"china\",\"age\":\"20\"}");
cout << test << endl;
test["name"] = "mike";
test["country"] = "USA";
cout << test << endl;
}
{
// {"book_name":"c++ primer 5th","price":"$19.99"}
json test("{\"book_name\":\"c++ primer 5th\",\"price\":\"$19.99\"}");
cout << test << endl;
test["page"] = "345";
test["ISBN"] = "978-962";
cout << test << endl;
}
{
int AvoidRepeatedData;
cin >> AvoidRepeatedData;
string rawString;
cin >> rawString;
json test(rawString);
cout << test << endl;
}
return 0;
}
LN : JSON (利用C++实现JSON)的更多相关文章
- Java下利用Jackson进行JSON解析和序列化
Java下利用Jackson进行JSON解析和序列化 Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...
- json处理三部曲之第三曲:利用Gson处理json
需要导入gson-xxx.jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifac ...
- Struts2.5 利用Ajax将json数据传值到JSP
AJAX +JSON=>JSP AJAX AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着 ...
- 如何利用JavaScript遍历JSON数组
1.设计源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- 利用Ajax和JSON实现关于查找省市名称的二级联动功能
功能实现的思路:我们经常碰见网上购物时候填写收件地址会用到这个查找省市县的三级联动查找功能,我们可以利用Ajax和JSON技术模拟这个功能,说白了同样是使用Ajax的局部数据更新功能这个特性.因为省市 ...
- Spring学习---Spring中利用jackson进行JSON转换
Spring中利用jackson进行JSON转换 import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExce ...
- 利用Redis撤销JSON Web Token产生的令牌
利用Redis撤销JSON Web Token产生的令牌 作者:chszs.版权全部.未经允许,不得转载.博主主页:http://blog.csdn.net/chszs 早先的博文讨论了在Angula ...
- Java基础/利用fastjson反序列化json为对象和对象数组
利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...
- 利用JsonSchema校验json数据内容的合规性(转)
原文地址:Json schema 背景: 复杂的AJAX应用程序可以与数百个不同的JSON服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...
随机推荐
- codevs——1275 有鱼的声音
1275 有鱼的声音 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Des ...
- Spring Boot多数据源连接8小时后断开的问题解决(MySQL)
这个问题涉及的方面很多,需要一步步去排查,可能环境有问题,数据库有问题,但是网上最多的应该是如下的方式去解决. 以单个数据源为主,多个数据源基本方法一致. 1.MySQL 5版本之前可以通过在URL后 ...
- sql 按中文排序
sql server:select * from [表名]order by [字段],[字段] collate Chinese_PRC_CS_AS_KS_WS mysql:select * from ...
- crontab not running
there are mutliple ways to describle this issue 1. crontab not running 2. crontab not running and no ...
- go-import下划线的作用
原文:http://studygolang.com/articles/4356 ------------------------------------------------------------ ...
- Unity3D 游戏引擎之C#使用Socket与HTTP连接server数据传输包
近期比較忙.有段时间没写博客拉.近期项目中须要使用HTTP与Socket.雨松MOMO把自己这段时间学习的资料整理一下. 有关Socket与HTTP的基础知识MOMO就不赘述拉,不懂得朋友自己谷歌吧. ...
- 函数绑定 bind
函数拓展-bind bind实现的是:对函数绑定作用域 更改作用域的方法:call,apply,with,eval,bind call 和 apply 的比较 相同点:1.都是在使用时候(使用即执行) ...
- 解决VS命令提示符 “Setting environment for using Microsoft Visual Studio. 此时不应有“系列错误
一.起因 近期在玩Boost库.当然首先是要进行Boost库的安装和配置.于是浅墨Google了一下boost库的安装配置攻略.下载了最新版1.55的boost库.就愉悦地開始进行配置了. 当进行到第 ...
- Velocity高速新手教程
变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號中的字面字符串将解析和又 ...
- 中文分词实践(基于R语言)
背景:分析用户在世界杯期间讨论最多的话题. 思路:把用户关于世界杯的帖子拉下来.然后做中文分词+词频统计,最后将统计结果简单做个标签云.效果例如以下: 兴许:中文分词是中文信息处理的基础.分词之后.事 ...