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
- 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;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号