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服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...
随机推荐
- 输入一个URL之后。。。
1.输入URL2.浏览器去浏览器缓存.系统缓存.路由器缓存查找缓存记录,有则直接访问URL对应的IP,无则下一步3.DNS解析URL,获得对应的IP4.浏览器通过TCP/IP三次握手连接服务器5.客户 ...
- 洛谷 P1457 城堡 The Castle
P1457 城堡 The Castle 题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票).结果这 ...
- Ansible 2.0公布
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/ansible-2-released 经过了一年的开发工作后,Ansib ...
- 关于 TCP 并发连接的几个思考题与试验
http://blog.csdn.net/solstice/article/details/6579232
- CodeIgniter 向mysql插入数据包括字母、汉字问题
今天在使用ci框架,须要向mysql数据表插入数据.当中的一个字段包括汉字.字母.但是用传统的使用sql语句:insert into XXX这样的方式,不管怎样都插入不成功,最后我换了还有一种方式: ...
- 【block第四篇】实现
-------------------------------------------欢迎查看block连载博客[专栏]--------------------------------------[b ...
- 找了两个node+vue-cli+mogodb的小项目学学
实现的博客系统,自己抽时间搭建看看 https://www.cnblogs.com/yesyes/p/7188331.html https://github.com/chaohangz/vueBlog
- MySql command line client 命令系列
—————————————————————————————————————————————————————————— 一.启动与退出 1.进入MySQL:启动MySQL Command Line Cl ...
- Redis源代码分析(六)--- ziplist压缩列表
ziplist和之前我解析过的adlist列表名字看上去的非常像.可是作用却全然不同.之前的adlist主要针对的是普通的数据链表操作. 而今天的ziplist指的是压缩链表.为什么叫压缩链表呢.由于 ...
- SSL和SSH的差别
有人说,SSH通常是用来提供安全的登录用的.SSL仅仅是一个在协议层中增加的一层用来提供安全. SSH工作在TCP之上,能够在启动一个SSH应用后.在其通道里执行其他协议的应用.如邮件. ...