0

我有以下 QString,我只想提取 access_token 值,即“http%3a%2f%2fschemas.xmlsoap .....” 怎么做?

{"token_type":"http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0","access_token":"http%3a%2f%2fschemas.xmlsoap.org%2fws% 2f2005%2f05%2fidentity%2fclaims%2fnameidentifier=asdasr21321214a%2f%2fschemas.microsoft.com%2faccwresscontrolservice%2f2010%2f07%2fclaims%2fidentityprovider=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&Audience=http%3a% 2f%2fapi.microsofttranslator.com&ExpiresOn=1347332993&Issuer=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&HMACSHA256=sFqvp2a2xXc3VBdxYZ6xHQf%2fKkOydnuX6VK7A6Yf55k%3d","expires_in":"599" api.microsofttranslator.com"}

4

3 回答 3

2

在http://qjson.sourceforge.net/上查看 QJson 。

您可以轻松地将字符串解析为标记化属性。从使用页面:

QJson::Parser parser;
QString json = "{your json above}";
bool ok;
QVariant result = parser.parse (json, &ok);
qDebug() << result["access_token"].toString();

玩得开心。

于 2012-09-11T03:20:46.977 回答
1

试试这个正则表达式:

\"access_token\":\"([^\"]*)\"

解释:

( subexpression )
Captures the matched subexpression and assigns it a zero-based ordinal number.

[^ character_group ]
Negation: Matches any single character that is not in character_group. By default, characters in character_group are case-sensitive.

于 2012-09-11T04:16:20.047 回答
1

又快又脏,仅用于 QString::section 的演示:

QString Data("{...you json data...}");
QString AccessToken = data.section("access_token\":\"",1).section("\"",0,0);

考虑:QString::section 缓慢而沉重,在后台创建 QStringList 并将标记转换为 RegExp。但在某些情况下,我仍然经常使用它。

于 2012-09-14T22:30:06.047 回答