zoukankan      html  css  js  c++  java
  • Qt解析王者荣耀英雄JSON文件

    JSON简介

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,使用了类C语言家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。

    JSON语法

    JSON语法是JavaScript对象表示法语法的子集,语法规则如下:

    ​ A、数据在key/value对中

    ​ B、数据由逗号分隔

    ​ C、花括号保存对象

    ​ D、方括号保存数组

    JSON名称/值对

    JSON 数据的书写格式是:key/value

    ​ 名称/值对包括字段名称(在双引号中),后面写一个冒号,然后是值:

    "firstName" : "John"

    JSON值

    JSON值类型:

    ​ 数字(整数或浮点数)

    ​ 字符串(在双引号中)

    ​ 逻辑值(true或false)

    ​ 数组(在方括号中)

    ​ 对象(在花括号中)

    ​ null

    JSON 对象

    JSON对象在花括号中书写:

    ​ 对象可以包含多个key/value对:

    { "firstName":"John" , "lastName":"Doe" }

    JSON 数组

    JSON数组在方括号中书写:

    ​ 数组可包含多个对象:

    { "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" }] }

    QT5解析Json文件

    使用到的类:

    QJsonArray 封装 JSON 数组
    QJsonDocument 读写 JSON 文档
    QJsonObject 封装 JSON 对象
    QJsonObject::iterator 用于遍历QJsonObject的STL风格的非const遍历器
    QJsonParseError 报告 JSON 处理过程中出现的错误
    QJsonValue 封装 JSON 值

    解析王者荣耀英雄人物

    Json文件地址:http://pvp.qq.com/web201605/js/herolist.json

    下载后的JSON文件(消减版)

    [{
    	"ename": 105,
    	"cname": "廉颇",
    	"title": "正义爆轰",
    	"new_type": 0,
    	"hero_type": 3,
    	"skin_name": "正义爆轰|地狱岩魂"
    }, {
    	"ename": 106,
    	"cname": "小乔",
    	"title": "恋之微风",
    	"new_type": 0,
    	"hero_type": 2,
    	"skin_name": "恋之微风|万圣前夜|天鹅之梦|纯白花嫁|缤纷独角兽"
    }]
    

    测试代码

    #include <QCoreApplication>
    #include <QJsonDocument>
    #include <QJsonArray>
    #include <QJsonObject>
    #include <QJsonParseError>
    #include <QJsonValue>
    #include <QStringList>
    #include <QFile>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QJsonParseError jsonError;
        QByteArray Json;
        QFile file("herolist.json");
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() << file.errorString();
        }
        else
        {
            qDebug() << "open sucess !";
            Json = file.readAll();
        }
        QString str = str.fromLocal8Bit(Json.data());
        //qDebug()<<Json.data();
        QJsonDocument doucment = QJsonDocument::fromJson(Json, &jsonError);
        if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
        {
            if (doucment.isArray())
            {
                qDebug()<<"Json数组读取";
                QJsonArray array = doucment.array();    /*获取QJsonArray对象*/
                int nSize = array.size();               /*Json数组大小*/
                for (int i = 0; i < nSize; ++i)
                {   // 遍历数组
                    QJsonValue value = array.at(i);
    
                    QJsonObject key = value.toObject(); /*获取数组对象*/
                    qDebug() << "ename==" << key["ename"].toInt();
                    qDebug() << "cname==" << key["cname"].toString();
                    qDebug() << "title==" << key["title"].toString();
                    qDebug() << "new_type==" << key["new_type"].toInt();
                    qDebug() << "hero_type==" << key["hero_type"].toInt();
                    qDebug() << "skin_name==" << key["skin_name"].toString();
                    qDebug() << "****************************************************************";
                }
            }
        }
        return a.exec();
    }
    
  • 相关阅读:
    jvm理论-运行时数据区
    java nio-理解同步、异步,阻塞和非阻塞
    真正的mybatis_redis二级缓存
    扩展 DbUtility (1)
    DbUtility v3 背后的故事
    DbUtility v3
    Jumony Core 3,真正的HTML引擎,正式版发布
    新项目,WebTest
    面试经验总结,每个求职者应该具有的职业素养
    mkdir()提示No such file or directory错误的解决方法
  • 原文地址:https://www.cnblogs.com/deroy/p/14067376.html
Copyright © 2011-2022 走看看