给出一个macchina.io复杂一点的JSON格式解析实例C++代码
时间: 2024-02-16 20:02:59 浏览: 79
当然,下面是一个简单的macchina.io的JSON格式解析实例C++代码:
```cpp
#include <iostream>
#include <vector>
#include <Poco/JSON/Parser.h>
#include <Poco/JSON/Object.h>
using namespace std;
using namespace Poco::JSON;
int main()
{
string jsonStr = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\",\"cars\":[\"Ford\",\"BMW\",\"Fiat\"]}";
Parser parser;
Dynamic::Var result = parser.parse(jsonStr);
Object::Ptr object = result.extract<Object::Ptr>();
string name = object->getValue<string>("name");
int age = object->getValue<int>("age");
string city = object->getValue<string>("city");
vector<string> cars;
Array::Ptr arr = object->getArray("cars");
for (size_t i = 0; i < arr->size(); i++)
{
string car = arr->get(i).toString();
cars.push_back(car);
}
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "City: " << city << endl;
cout << "Cars: ";
for (size_t i = 0; i < cars.size(); i++)
{
cout << cars[i] << " ";
}
cout << endl;
return 0;
}
```
这段代码演示了如何使用macchina.io的JSON解析器解析一个JSON对象,并将其转换为C++对象。在这个例子中,我们将一个包含名称、年龄、城市和汽车的JSON字符串解析成C++对象,并打印出来。
阅读全文