本文转载自:http://blog.sina.com.cn/s/blog_671732440100uwxh.html

使用qtscipt库解析json数组
首先在工程文件中加

QT        += script

然后头文件加

#include <QtScript>

然后解析

QString str = "{\"name\":\"xiaotang\", \"age\":\"23\", \"chi\":[{\"a\":\"aa\", \"b\":\"bb\"}, {\"a\":\"aaa\", \"b\":\"bbb\"}]}";

QScriptEngine engine;

QScriptValue sc = engine.evaluate("value="+str);

qDebug() << sc.property("name").toString(); //解析字段

if(sc.property("chi").isArray()) //解析数组

{

QScriptValueIterator it(sc.property("chi"));

while (it.hasNext())

{

it.next();

if(!it.value().property("a").toString().isEmpty())

qDebug() << it.value().property("a").toString();

}

}

自己在Win7环境下安装了Qt4.8.5库qt-win-opensource-4.8.5-vs2008.exe和QtCreator编辑器qt-creator-windows-opensource-2.8.0.exe,采用VS2008替代MinGW作为编译器。如果想在VS2008下部署和开发QT应用的话,再从QT官网下载一个插件qt-vs-addin-1.1.11-opensource.exe就OK了,不过VS2008对QT库的函数自动提示支持等方面没有QTCreator做得好。以上的软件都可以从QT官网Download页http://qt-project.org/downloads下载。

QT代码如下:

#include <QtGui/QApplication>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue>
#include <QtScript/QScriptValueIterator>
#include <QtCore/QtDebug> int main(int argc, char *argv[])
{
QApplication app(argc, argv); QString strJson = "{\"department\":\"computer science\", \"person number\":\"45\", \"student\":[{\"Name\":\"ccf\",\"Math\":\"95\", \"Chinese\":\"84\"}, {\"Name\":\"zhangsan\",\"Math\":\"75\", \"Chinese\":\"70\"}]}";
QScriptEngine engine;
QScriptValue sc = engine.evaluate("value=" + strJson);
qDebug() << "department:" << sc.property("department").toString();//解析字段
if (sc.property("student").isArray()) //解析数组
{
QScriptValueIterator it(sc.property("student"));
while(it.hasNext())
{
it.next();
if (!it.value().property("Name").toString().isEmpty())
{
qDebug() << "Name :" << it.value().property("Name").toString();
qDebug() << "Math :" << it.value().property("Math").toString();
qDebug() << "Chinese :" << it.value().property("Chinese").toString();
}
}
} return app.exec();
}

运行结果如下图所示:

   主要用到QtScript库的三个类:QScriptEngine、QScriptValue、QScriptValueIterator类,具体使用方法可以查看QT帮助文档。

http://blog.csdn.net/ccf19881030/article/details/12527607

使用QtScript库解析Json数组例子的更多相关文章

  1. Jquery解析json数组字符串

    最近在工作中用到了Jquery来解析json字符串,网上解析jquery解析json单个对象的实例不少,但是jquery解析json数组的实例却是不多,下面我举一个简单的例子来跟大家分享与一下,本人水 ...

  2. 如何在 Apache Hive 中解析 Json 数组

    我们都知道,Hive 内部提供了大量的内置函数用于处理各种类型的需求,参见官方文档:Hive Operators and User-Defined Functions (UDFs).我们从这些内置的 ...

  3. 解析json数组

    解析json数组 JSONArray jsonArray = new JSONArray(markingStr); int iSize = jsonArray.length(); for (int i ...

  4. Gson解析Json数组

    需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库 Json数据是这样的 { "result": { "heroes": [ { &quo ...

  5. java解析json数组

      java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...

  6. hive中解析json数组

    -- hive中解析json数组 select t1.status ,substr(ss.col,,) as col ,t3.evcId ,t3.evcLicense ,t3.evcAddress , ...

  7. fastjson解析json数组

    1.fastjson解析json数组(直接上代码) import java.util.ArrayList; import java.util.List; import com.alibaba.fast ...

  8. 前端学习之——js解析json数组

    ** 前端学习之——js解析json数组** 解析json数组即对JSONArray的遍历 一.对于标准的json数组如: var result=[{"flag":1," ...

  9. C#解析JSON数组

    方式一 第一步:使用前,需下载:Newtonsoft.Json.dll 没有的,请到我百度云盘下载 链接:https://pan.baidu.com/s/1JBkee4qhtW7XOyYFiGOL2Q ...

随机推荐

  1. AngularJs中文社区学习资料

    AngularJs中文社区学习资料,供学习: http://angularjs.cn/tag/AngularJS

  2. [转载]STL map中的一些基本函数

    来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博客 Map是c++的一个标准容器 ...

  3. spring读取properties文件

    1.方式一 <util:properties id="meta" location="classpath:config/metainfo.properties&qu ...

  4. Objective-C实现变参函数

    原文:http://www.tanhao.me/pieces/1104.html   NSLog(NSString *format, ...)   + (id)arrayWithObjects:(id ...

  5. 并发队列之:BlockingQueue和ConcurrentLinkedQueue

    一.并行和并发区别: 并行:是指两者同时执行一件事.比如赛跑,两个人都在不停的往前跑: 并发:是指资源有限的情况下,两者交替轮流使用资源.比如一段路(单核CPU资源)同时只能过一个人,A走一段后,让给 ...

  6. win10 pro eclipse maven: Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:mav invalid END header (bad central directory offset)

    Error:Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:mav ... invalid E ...

  7. MyBatis的事务处理

    先来假设这样一个问题:如果数据库里面有一个用户表和一个作家表,那么当要添加一条数据到作家表中时,作家表的id必须是用户表中的其中一个id,因为作家一定也要是一个用户.这时就涉及到事务处理. 在上一篇博 ...

  8. 获取程序中无需释放的ViewController

    AppDelegate.h @property (strong, nonatomic) UIViewController *viewController; 在需要用的地方 #define appDel ...

  9. pptv web前端面试题答案

    这是星期一考完试,答应星期三补上的,代码很简单,就不写注释了 //php快排 function quickSort(&$arr){   $arr_left=new array();   $ar ...

  10. JavaScript兼容问题汇总[实时更新]

    印象笔记链接地址:点我查看   遇到问题不断更新中--    [转载请注明出处-HTML5自由者]