Parsing JSON with QT using standard QT library
来源:互联网 发布:淘宝动漫周边店成本 编辑:程序博客网 时间:2024/04/30 20:55
First time i find out this it took a while. But actually there is no need any Additional code except QT itself, unless you want gain more performance then QT standard library gives. So if you want to parse JSON object first you have to include QT script engine.
#include <QScriptEngine>
Let say PHP returns something like this.
echo json_encode(array('result' => array(1,2,3)));
So C++ code looks like:
QByteArray result; result = QhttpClient->readAll(); QScriptValue sc; QScriptEngine engine; sc = engine.evaluate(QString(result)); // In new versions it may need to look like engine.evaluate("(" + QString(result) + ")"); if (sc.property("result").isArray()) { QStringList items; qScriptValueToSequence(sc.property("result"), items); foreach (QString str, items) { qDebug("value %s",str.toStdString().c_str()); } }
In this example this code writes something like: value 1 value 2 value 3
values comes from generated PHP script. Let say php now gives us back something like:
echo json_encode(array('error' => false));
In this case just more simple syntax.
if (sc.property("error").toBoolean() == true) qDebug("Error detected"); else qDebug("All ok");
And finally most complex example witch should be ready to use for any purpose. Let say PHP gives something like this:
echo json_encode( array('result' => array ( array('id' => '1' ,'date' => '487847','nick' => 'Remigiijus'), array('id' => '1' ,'date' => '487847','nick' => 'remdex') ) ));
In this case we must include additional file:
#include <QScriptValueIterator>
And let say we want to print nick names. So our C++ file will look like.
QByteArray result; result = QhttpClient->readAll(); QScriptValue sc; QScriptEngine engine; sc = engine.evaluate(QString(result));// In new versions it may need to look like engine.evaluate("(" + QString(result) + ")"); if (sc.property("result").isArray()) { QScriptValueIterator it(sc.property("result")); while (it.hasNext()) { it.next(); qDebug("Nick %s",it.value().property("nick").toString().toStdString().c_str()); } }
In this case we just use QScriptValueIterator and iterate through array. Anyway from this point everything is possible :)
- Parsing JSON with QT using standard QT library
- Parsing JSON with QT using standard QT library
- Using OpenCV with Qt
- How-to: parsing XML with Qt
- Using Qt Extended 4.4 with Qt 4.5
- Parsing JSON With SBJSON
- Using standard library header files
- Using Qt 4.4 with Visual Assist X
- Using the Maya API with Qt Creator
- Using the Qt Creator IDE with non Qt projects
- Cannot mix incompatible Qt library (version ) with this library (version )
- Python Standard Library - JSON Usage
- QT入门,while parsing target library list: not well-formed (invalid token)解决
- qt配置--qt library qt creator
- Qt json
- QT JSON
- QT json
- Qt--JSON
- 算法导论 4.1-5
- 解决TXT乱码问题
- QJson 使用
- 用友CDM系统,将货位间商品移库单(一步)修改为内调出入库单(一步)方法使用
- 在DP中使用"滚动数组"
- Parsing JSON with QT using standard QT library
- 409. The fox preys farest from home. 兔子不吃窝边草
- Linux的分页机制
- JSON Spirit 中文支持(转)
- 设置win7任务栏程序图标显示方式
- IT技术人的成长
- Deploy JAX-WS Web Services On Tomcat
- qt解析json(使用第三方库
- Pages Library不能显示Library Settings页面的错误处理