怎样用C++写一个json解析类
以下是一个简单的C++ JSON解析类的示例代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class JsonParser {
private:
string json;
int index;
public:
JsonParser(string json) {
this->json = json;
this->index = 0;
}
bool isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
void skipWhiteSpace() {
while (index < json.length() && isWhiteSpace(json[index])) {
index++;
}
}
string parseString() {
string result = "";
index++; // skip opening quote
while (index < json.length()) {
char c = json[index++];
if (c == '"') {
return result;
} else if (c == '\\') {
char next = json[index++];
if (next == 'n') {
result += '\n';
} else if (next == 't') {
result += '\t';
} else {
result += next;
}
} else {
result += c;
}
}
return result;
}
double parseNumber() {
string numStr = "";
bool isNegative = false;
if (json[index] == '-') {
isNegative = true;
index++;
}
while (index < json.length() && isdigit(json[index])) {
numStr += json[index++];
}
if (index < json.length() && json[index] == '.') {
numStr += '.';
index++;
while (index < json.length() && isdigit(json[index])) {
numStr += json[index++];
}
}
return isNegative ? -1 * stod(numStr) : stod(numStr);
}
bool parseBoolean() {
if (json[index] == 't') {
index += 4; // true
return true;
} else {
index += 5; // false
return false;
}
}
void skipNull() {
index += 4; // null
}
vector<JsonParser> parseArray() {
vector<JsonParser> result;
index++; // skip opening bracket
skipWhiteSpace();
while (index < json.length() && json[index] != ']') {
result.push_back(parseValue());
skipWhiteSpace();
if (index < json.length() && json[index] == ',') {
index++;
skipWhiteSpace();
}
}
index++; // skip closing bracket
return result;
}
map<string, JsonParser> parseObject() {
map<string, JsonParser> result;
index++; // skip opening brace
skipWhiteSpace();
while (index < json.length() && json[index] != '}') {
string key = parseString();
skipWhiteSpace();
index++; // skip colon
skipWhiteSpace();
result[key] = parseValue();
skipWhiteSpace();
if (index < json.length() && json[index] == ',') {
index++;
skipWhiteSpace();
}
}
index++; // skip closing brace
return result;
}
JsonParser parseValue() {
skipWhiteSpace();
char c = json[index];
if (c == '{') {
return parseObject();
} else if (c == '[') {
return parseArray();
} else if (c == '"') {
return parseString();
} else if (isdigit(c) || c == '-') {
return parseNumber();
} else if (c == 't' || c == 'f') {
return parseBoolean();
} else if (c == 'n') {
skipNull();
return NULL;
} else {
throw "Invalid JSON";
}
}
};
int main() {
string jsonStr = R"(
{
"name": "John Smith",
"age": 42,
"isMarried": true,
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
},
"phoneNumbers": [
"555-1234",
"555-5678"
]
}
)";
JsonParser parser(jsonStr);
map<string, JsonParser> obj = parser.parseObject();
cout << "Name: " << obj["name"].parseString() << endl;
cout << "Age: " << obj["age"].parseNumber() << endl;
cout << "Is Married: " << obj["isMarried"].parseBoolean() << endl;
map<string, JsonParser> address = obj["address"].parseObject();
cout << "Street: " << address["street"].parseString() << endl;
cout << "City: " << address["city"].parseString() << endl;
cout << "State: " << address["state"].parseString() << endl;
vector<JsonParser> phoneNumbers = obj["phoneNumbers"].parseArray();
cout << "Phone Numbers: ";
for (JsonParser phoneNumber : phoneNumbers) {
cout << phoneNumber.parseString() << " ";
}
cout << endl;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/dnAp 著作权归作者所有。请勿转载和采集!