{
    "student": "学生事务管理系统",
    "version": 500,
    "windows": true
}

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QStringList>
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QByteArray>
#include <QString>
#include <QJsonParseError>
#include <QJsonArray>
#include <QFile>

#include <qtextcodec.h>
#pragma execution_character_set("utf-8")
QByteArray byte_array;
QByteArray byte_array1;



void createJson()
{
   QJsonObject json;
   json.insert("version", 500);
   json.insert("windows", true);
   json.insert ("student", (QStringLiteral("学生事务管理系统")));

   QJsonDocument document;
   document.setObject(json);
   byte_array= document.toJson();
   QString json_str = QString::fromLocal8Bit(byte_array);



QFile t_file("D:/00742672/Desktop/1.json");      //生成一个文件
if(!t_file.open(QIODevice::WriteOnly))
{
  qDebug() << "打开文件失败";
}
QTextStream t_stream(&t_file);
t_stream<<json_str;
t_file.close(); //关闭文件


}


void paserJsonObject(QString f)   //解析dev1.json
{
   QString value= ReadFile(f);
   QJsonParseError json_error;
   QJsonDocument parse_doucment = QJsonDocument::fromJson(value.toUtf8(), &json_error);
   qDebug()<<"json_error.error"<<json_error.error;
   if(json_error.error == QJsonParseError::NoError)
   {
       if(parse_doucment.isObject())
       {

           QJsonObject obj = parse_doucment.object();
           if(obj.contains("student"))
           {
            QJsonValue name_value = obj.take("student");
             if(name_value.isString())
          {
             QString name = name_value.toString();  /*.toLocal8Bit().data()*/;
             qDebug()<<"student="<<name;
          }
           }
         if(obj.contains("version"))
   {

       QJsonValue version_value = obj.take("version");
       if(version_value.isDouble())
       {
   int version = version_value.toVariant().toInt();
    qDebug()<<"version="<<version;
       }
   }
   if(obj.contains("windows"))
   {
       QJsonValue version_value = obj.take("windows");
       if(version_value.isBool())
       {
   bool flag = version_value.toBool();
   qDebug()<<"windows="<<flag;
       }
   }
       }
   }

}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    createJson();
    paserJsonObject("D:/00742672/Desktop/1.json");
    return a.exec();
}



https://blog.csdn.net/lidandan2016/article/details/86404757 解决中文乱码


QString::fromLocal8Bit("提示")

不过在Qt5中,提供了一个专门的处理宏,来支持中文常量,那就是QStringLiteral,但它只能处理常量。
QMessageBox::information(this, QString::fromLocal8Bit("提示"), QStringLiteral("中文显示"));



Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐