如何读取一个本地Json文件并查询该文件展示其内容

import QtQuick 2.0import Ubuntu.Components 1.1import "jsonparser.js" as APIimport "jsonpath.js" as JSONPath/*!\brief MainView with a Label and Button elements.*/MainView {id: main// objectName for functional testing purposes (autopilot-qt5)objectName: "mainView"// Note! applicationName needs to match the "name" field of the click manifestapplicationName: "readjsonfile.liu-xiao-guo"/*This property enables the application to change orientationwhen the device is rotated. The default is false.*///automaticOrientation: true// Removes the old toolbar and enables new features of the new header.useDeprecatedToolbar: falsewidth: units.gu(60)height: units.gu(85)function update(json) {console.log("it is called in update!");// we can start to interpret the returned jsonvar authors = JSONPath.jsonPath(json, "$.store.book[*].author");console.log("length: " + authors.length);for (var i in authors ) {console.log("author[" + i +"] " + authors[i]);mymodel.append( { "content": authors[i],"type": "Authors"});}// Get all of the booksvar books = JSONPath.jsonPath(json, "$.store.book[*].title");console.log("length: " + books.length);for (var j in books ) {console.log("author[" + j +"] " + books[j]);mymodel.append( { "content": books[j],"type": "Books"});}}Page {id: mainPagetitle: i18n.tr("Read Json File")ListModel {id: mymodel}Component {id: sectionHeadingRectangle {width: mainPage.widthheight: childrenRect.heightcolor: "lightsteelblue"Text {text: sectionfont.bold: truefont.pixelSize: 20}}}ListView {id: listviewanchors.fill: parentmodel: mymodeldelegate: Text {text: content}section.property: "type"section.criteria: ViewSection.FullStringsection.delegate: sectionHeading}Component.onCompleted: {console.log("Start to read JSON file");API.readJsonFile("sample.json", main)}}}

为了完成我们的实验,我们也创建了一个sample.json文件。内容如下:

{ "store": {"book": [{"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{"category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{"category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95} }}

运行我们的应用,,我们应用显示的结果如下:

艰苦能磨练人的意志。

如何读取一个本地Json文件并查询该文件展示其内容

相关文章:

你感兴趣的文章:

标签云: