qt string qstring 解析

    我们先分别讨论下python中的string和Qstring

    1. Qstring

        The QString class provides a Unicode character string.

     Qstring是一个unicode编码的string

2.string

String literals are written in single or double quotes: 'xyzzy'"frobozz".Unicode strings are much like strings, but are specified in the syntax using a preceding 'u' character: u'abc'u"def"

可以看出string可以采用asc编码和unicode编码,如果不特别指名,则肯定为Asc-ii编码

 2.1 str()

Return a string containing a nicely printable representation of an object. For strings, this returns the string itself.its goal is to return a printable string. If no argument is given, returns the empty string, ''.

2.2 repr()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt4中,可以使用第三方库QJson来解析JSON数据。下面是一个简单的示例代码: ```cpp #include <QCoreApplication> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\",\"cars\":[\"Ford\",\"BMW\",\"Fiat\"]}"; // 解析JSON数据 QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8()); if (!jsonDoc.isNull()) { if (jsonDoc.isObject()) { QJsonObject jsonObj = jsonDoc.object(); // 获取name字段的值 QString name = jsonObj["name"].toString(); qDebug() << "Name: " << name; // 获取age字段的值 int age = jsonObj["age"].toInt(); qDebug() << "Age: " << age; // 获取city字段的值 QString city = jsonObj["city"].toString(); qDebug() << "City: " << city; // 获取cars数组的值 QJsonArray carsArray = jsonObj["cars"].toArray(); QStringList carsList; foreach (const QJsonValue &value, carsArray) { carsList.append(value.toString()); } qDebug() << "Cars: " << carsList; } } return a.exec(); } ``` 在上面的示例中,我们使用QJsonDocument::fromJson()函数将JSON字符串转换为QJsonDocument对象。然后,我们可以使用QJsonObject和QJsonArray来访问和解析JSON对象和数组中的数据。通过使用相应的字段名称,我们可以获取JSON中的值,并将其转换为适当的类型。 请注意,Qt4的QJson模块不是内置的,需要单独安装。你可以从Qt官方网站或第三方库管理工具中获取QJson模块,并将其添加到你的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值