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服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...
随机推荐
- Linux下tomcat的catalina.out屏蔽
修改catalina.sh ,找到下面的位置: if [ -z "$CATALINA_OUT" ] ; then#CATALINA_OUT="$CATALINA_BASE ...
- 立面图 平面图 剖面图 CAD
http://www.qinxue.com/88.html http://www.xsteach.com/course/2855 前后左右各个侧面的外部投影图——立面图:对建筑物各个侧面进行投影所得到 ...
- 005 EIGRP
Router>en Router#config t Enter configuration commands, one per line. End with CNTL/Z. Router(co ...
- [Sqlite3] Sqlite Introduction
Check whether you have sqlite3 installed: sqlite3 -version To create a new db: sqlite3 <filename. ...
- JSP自己定义标签入门实例具体解释
JSP自己定义标签主要能用到的两个包 javax.servlet.jsp.*;javax.servlet.jsp.tagext.*; 自己定义标签<userInfo:showUserInfo/& ...
- 【剑指Offer】俯视50题之31 - 40题
面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...
- .NET Core/.NET之Stream简介 Rx.NET 简介
.NET Core/.NET之Stream简介 之前写了一篇C#装饰模式的文章提到了.NET Core的Stream, 所以这里尽量把Stream介绍全点. (都是书上的内容) .NET Core ...
- C语言之基本算法25—牛顿迭代法求方程近似根
//牛顿迭代法! /* ============================================================ 题目:用牛顿迭代法求解3*x*x*x-2*x*x-16 ...
- Android自己定义组件系列【5】——进阶实践(2)
上一篇<Android自己定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这 ...
- 树状数组 LA 4329 亚洲赛北京赛区题
复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...