【问题标题】:Qt QSqlQuery return in jsonQT SqlQuery 以 json 格式返回
【发布时间】:2013-08-06 04:56:21
【问题描述】:

我在我的应用程序中创建了一个 sqlquery 方法,它基本上获取一个 SQL 命令并以 json 格式返回结果,问题是当填充 " 和其他有问题的字符时,这会创建错误的 jsons..

我尝试先创建一个QObject,然后将其序列化为JSON,但无法实现。

即使数据包含“符号”,如何使该方法生成有效的json?

QString Api::SQLQuery(const QString & sqlquery)
{
QSqlQuery query;

bool firstline = true;
query.setForwardOnly(true);
if(query.exec(sqlquery))
{
    QString answer = "[";
    while(query.next())
        {
            if(firstline){firstline = false;}else {answer += ",";}

            answer += "{";
            for(int x=0; x < query.record().count(); ++x)
            {
                if(x != 0){answer += ",";}
                answer += "\""+query.record().fieldName(x) +"\":\""+ query.value(x).toString()+"\"";
            }
            answer += "}";
        }
    answer += "]";
    return answer;
}
else
{
    return query.lastError().text() ;
}

}

解决方案:

感谢答案,这是正确的方法:

QString Api::SQLQuery(const QString & sqlquery) {
QSqlQuery query;
  query.setForwardOnly(true);
  if (!query.exec(sqlquery))return QString();

  QJsonDocument  json;
  QJsonArray     recordsArray;

  while(query.next()) 
  {
     QJsonObject recordObject;
        for(int x=0; x < query.record().count(); x++)
        {
        recordObject.insert( query.record().fieldName(x),QJsonValue::fromVariant(query.value(x)) );
        }
     recordsArray.push_back(recordObject);
  }
  json.setArray(recordsArray);

  return json.toJson();
}

【问题讨论】:

    标签: c++ mysql json qt qsqlquery


    【解决方案1】:

    小设计说明.. 我建议审查有关错误处理的设计。您正在从您的函数返回 QString ,它可以是正确的 JSON 文档或只是错误文本。因此,您实际上在一种语言类型(字符串)中混合了不同的结果集类型。 因此,您需要在代码中进行一些额外的检查,以了解实际发生的情况。

    Qt 5.x 示例:

    QString Api::SQLQuery(const QString & sqlquery) {
      QSqlQuery query;
    
      query.setForwardOnly(true);
      if (!query.exec(sqlquery))
          return QString();
    
      QJsonDocument  json;
      QJsonArray     recordsArray;
    
      while(query.next()) {
         for(int x=0; x < query.record().count(); x++) {
             QJsonObject        recordObject;
    
         recordObject.insert( query.record().fieldName(x), 
                   QJsonValue::fromVariant(query.value(x)) );   
         }
         recordsArray.push_back(recordObject);
      }
      json.setArray(recordsArray);
    
      return json.toJson();
    

    }

    【讨论】:

    • 谢谢!我想出了如何按照我的想法实现它。 2 处更正: json.setObject 应该是 json.setArray 并且进入 for 循环的 push_back 应该在循环之外,但在 while 循环中。我在原始帖子中添加了更正的代码。再次感谢。
    【解决方案2】:

    我建议使用正确的 Json 实现来正确地转义引号等。

    如果您使用的是 Qt5:Qt5 附带 QJsonDocument 作为 qtbase 的一部分。

    如果您使用的是 Qt4:没有内置 Json 支持,但您可以使用第三方库,例如 qjson

    如果你真的不能使用合适的库,你可以自己动手,手动转义特殊字符 (Here's a list)。

    例如

    QString escapeForJson(QString s) {
        s = s.replace(QLatin1String("\""), QLatin1String("\\\"));
        …
        return s;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 2011-06-11
      • 2011-01-08
      • 2021-12-20
      • 2014-07-14
      • 2017-02-24
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多