easiest way to parse JSON in Qt 4.7

后端 未结 6 2018
甜味超标
甜味超标 2020-12-13 09:44

I need to parse JSON object through Qt. What is the easiest/fastest way to do it?

6条回答
  •  粉色の甜心
    2020-12-13 10:40

    JSON parsing is now supported in Qt 5. Here's how to load and parse a document:

    #include 
    #include 
    #include 
    #include 
    
    // ...
    
    // Read JSON file
    QFile file("/path/to/file.json");
    file.open(QIODevice::ReadOnly);
    QByteArray rawData = file.readAll();
    
    // Parse document
    QJsonDocument doc(QJsonDocument::fromJson(rawData));
    
    // Get JSON object
    QJsonObject json = doc.object();
    
    // Access properties
    qDebug() << json["something"].toString();
    

提交回复
热议问题