zoukankan      html  css  js  c++  java
  • 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

    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 << "{
    ";
            for (it = data.begin(); it != data.end(); it++) {
                num++;
                os << "    "" << it->first << "": "" << it->second << """;
                if (num != obj.count()) {
                    os << ",";
                }
                os << "
    ";
            }
            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;
    }
  • 相关阅读:
    海康、大华摄像头RTSP接入实现WEB端无插件流媒体服务EasyNVR实现海康大华宇视摄像头内容网页播放的方法
    【 D3.js 高级系列 — 6.0 】 值域和颜色
    物联网操作系统HelloX V1.78测试版正式发布
    【 D3.js 高级系列 — 5.1 】 颜色插值和线性渐变
    清华尹成老师主办的智锋互联
    【 随笔 】 D3 难吗?
    禅道,然之和蝉知入驻VM Depot
    【 D3.js 高级系列 — 5.0 】 颜色
    尹成老师主办培训教学机构 智锋互联 传播智慧我们用心,人生冲锋你们用心
    体验Azure的 Automation “自动化” 服务预览版
  • 原文地址:https://www.cnblogs.com/renleimlj/p/5525591.html
Copyright © 2011-2022 走看看