• 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

  1. You just need to finish the constructor,which find out the key/value pairs and store in _data
  2. all the string doesn't consist of space(空格), and it is strictly formed like {"key1":"value1","key2":"value2","key3":"value3"}
  3. all the key and value have double quotation marks(双引号)
  4. in front of them and after them(所有键的前后和值的前后都有双引号)
  5. 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)的更多相关文章

  1. Java下利用Jackson进行JSON解析和序列化

    Java下利用Jackson进行JSON解析和序列化   Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行 ...

  2. json处理三部曲之第三曲:利用Gson处理json

    需要导入gson-xxx.jar包 <dependency> <groupId>com.google.code.gson</groupId> <artifac ...

  3. Struts2.5 利用Ajax将json数据传值到JSP

    AJAX +JSON=>JSP AJAX AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着 ...

  4. 如何利用JavaScript遍历JSON数组

    1.设计源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  5. 利用Ajax和JSON实现关于查找省市名称的二级联动功能

    功能实现的思路:我们经常碰见网上购物时候填写收件地址会用到这个查找省市县的三级联动查找功能,我们可以利用Ajax和JSON技术模拟这个功能,说白了同样是使用Ajax的局部数据更新功能这个特性.因为省市 ...

  6. Spring学习---Spring中利用jackson进行JSON转换

    Spring中利用jackson进行JSON转换 import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExce ...

  7. 利用Redis撤销JSON Web Token产生的令牌

    利用Redis撤销JSON Web Token产生的令牌 作者:chszs.版权全部.未经允许,不得转载.博主主页:http://blog.csdn.net/chszs 早先的博文讨论了在Angula ...

  8. Java基础/利用fastjson反序列化json为对象和对象数组

    利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...

  9. 利用JsonSchema校验json数据内容的合规性(转)

    原文地址:Json schema 背景: 复杂的AJAX应用程序可以与数百个不同的JSON服务进行交互,因此,引入对客户端验证的需求. 在处理校验问题方面有着很多的工具,但是通常可以将它们归为以下几类 ...

随机推荐

  1. MySQL查询去重语句

    1.distinct select count(distinct CName) from Course select count(CName) from (select distinct CName ...

  2. 【SQL Server 学习系列】-- 清除SQL Server 2012连接过的服务器名称

    需要删除两个地方,删除前请注意备份! 1.  %AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin 2.  %A ...

  3. CSS头像右上角的讨厌红点

    就是这个讨厌的红点,如图: 说明: 1.主要用到position定位: 2.使用border-radius画圆角: 源码: <!DOCTYPE html> <html> < ...

  4. 智能眼镜技术科普:VR、AR、MR的区别

    前段时间, 获得谷歌5亿美元融资的技术公司Magic Leap在WSJD展会中放出了一段实录视频,引起不小骚动.如今,也有媒体称他们为MR公司,那么VR.AR.MR之间到底有什么区别呢. VR.AR. ...

  5. 【python自制】让大白成为你的个人助手!

    我做这个软件就是要让卖萌进行究竟! 官方站点:http://www.jackeriss.com/companions.htm GitHub:https://github.com/Jackeriss/C ...

  6. [Maid] Write Tasks in Markdown with Maid

    Maid enables you to write your tasks in Markdown. Create a maidfile.md or a README.mdthen add Header ...

  7. MyEclipse 8.5安装Aptana

    Aptana简单介绍 Aptana是一个很强大,开源,专注于JavaScript的Ajax开发IDE它的特性包含:    1.JavaScript,JavaScript函数,HTML,CSS语言的Co ...

  8. firewalld 防火墙 nat 网络地址转换

    目的:实现以下效果 一. 准备环境 @1 三台虚拟机 @2  client 端 ip  192.168.1.2      server端   两块网卡 , ip 分别是 192.168.1.1   和 ...

  9. c#如何设置成:【当前打开的项目是什么,就默认它为启动项目】,不然新添或打开别的项目都要设置一次启动 [原创]VS2012中将当前选定项目做为启动项

    主菜单→[工具]→[选项]→[项目和解决方案]→[生成并运行],选中“对于新解决方案,使用当前选定的项目作为启动项目” 应该是右键单击解决方案,点击属性打开,选中“当前选定内容”那一项,就可以把你正在 ...

  10. java UDP聊天与文件传输

    package rgy.com.UDP3; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.Action ...