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服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...
随机推荐
- iis站点内存泄漏问题分析
在一次上线过程中iis内存飙升,随后跟运维要到站点的dump文件,使用windbg分析了clr的内存分配,找到了问题的症结,先记录如下: 使用windbg加载dump文件 1.打开windbg,Fil ...
- Ubuntu 16.04出现chmod: 无效模式:"a"的问题解决
命令: chmod a+x file1 提示:注意文件的类型,如果用在文件夹上是不行的,但是文件确实可以的.
- JFinal Weixin 微信极速 SDK
原文:https://git.oschina.net/jfinal/jfinal-weixin
- Spark修炼之道(基础篇)——Linux大数据开发基础:第二节:Linux文件系统、文件夹(一)
本节主要内容 怎样获取帮助文档 Linux文件系统简单介绍 文件夹操作 訪问权限 1. 怎样获取帮助文档 在实际工作过程其中,常常会忘记命令的使用方式.比如ls命令后面能够跟哪些參数,此时能够使用ma ...
- android/java经常使用的工具类源代码
anroid.java经常使用的工具类源代码,当中包含文件操作.MD5算法.文件操作.字符串操作.调试信息log.base64等等. 下载地址:http://download.csdn.net/det ...
- 自己动手写Android数据库框架
前言 相信不少开发人员跟我一样,每次都非常烦恼自己写数据库,并且那些数据库语句也经常记不住.当然网上也有非常多非常好的数据库框架,你能够直接拿来用,可是 非常多时候我们的项目.特别是一个小型的Andr ...
- 小工具:天气查询 Vs自定义设置 DevGridControl中GridView排序问题 小工具:火车票查询 小工具:邮件发送 小工具:截图&简单图像处理
小工具:天气查询 开发一个天气查询的工具主要由两步构成,一是数据的获取,二是数据的展示. 一.数据获取 数据获取又可以分为使用其它公司提供的API和手动抓取其它网站数据. 1. 某公司提供的AP ...
- [办公自动化]如何让excel图表标签中显示最新值数据
同事做了一张excel图表,希望最新的数据显示数据标签,其他都不显示.并且当单元格的数据新增加时,这个标签要能自动更新. 这里需要用到公式,获取到这个最新值.在b2输入公式=lookup(9e+307 ...
- HDU5806 NanoApe Loves Sequence Ⅱ
NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Ja ...
- Linux/Android——输入子系统input_event传递 (二)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/42099381 在前文Linux/Android——usb触摸屏驱动 - usbtouchscre ...