如何讀取乙個本地Json檔案並查詢該檔案展示其內容

2021-07-02 13:21:45 字數 1525 閱讀 7779

我前一段時間在我的部落格裡寫了一篇文章「如何在qml應用中讀寫檔案」,那篇文章是介紹如何使用c++來讀取檔案的。那種方法是乙個比較通用的方法。但是對於有些應用來說,我們可以通過配置json來建立我們的ui,或者對不同的平台進行配置,而不用寫乙個單獨的設定檔案來做這件事。那麼我們如何不需要通過c++的方法來讀取json檔案呢?

我們可以使用我們的sdk建立乙個最基本的qml應用。為了能夠讀取json檔案,我們建立乙個叫做「jsonparser.js」的檔案:

/* based on:

* jsonlistmodel - a qml listmodel with json and jsonpath support

* * licensed under the mit licence

* (*

*/"jsonpath.js")

function readjsonfile(source, callback)

}xhr.send();

}

我們通過xmlhttprequest來完成本地檔案的請求工作,並使用callback中的update來傳人到qml檔案中進行解析。為了能夠更加方便地解析我們得到的json檔案,我們使用了

為了顯示我們的jso資料,我們對我們的main.qml進行了修改:

import qtquick 2.0

import ubuntu.components 1.1

import "jsonparser.js" as api

import "jsonpath.js" as jsonpath

/*! \brief mainview with a label and button elements.

*/mainview );

}// get all of the books

var books = jsonpath.jsonpath(json, "$.store.book[*].title");

console.log("length: " + books.length);

for (var j in books ) );}}

page

component }}

listview

section.property: "type"

section.criteria: viewsection.fullstring

section.delegate: sectionheading

}component.oncompleted: }}

為了完成我們的實驗,我們也建立了乙個sample.json檔案。內容如下:

,,,

],"bicycle":

}}

執行我們的應用,我們應用顯示的結果如下:

所有的原始碼在:

讀取本地json檔案,解析json

data.json 檔案同目錄下 import json 引入模組 count 1 開啟乙個json檔案 data open data.json encoding utf 8 轉換為python物件 strjson json.load data flag false lockflag false w...

php讀取本地json檔案

1.data.json檔案 2.php檔案 echo 獲取頁面傳來的引數 type get type proid get id echo type.產品type echo proid.產品id 從檔案中讀取資料到php變數 json string file get contents json dat...

讀取本地的json檔案

最近寫專案需要讀取本地的json檔案,然後悲催的發現前端新手的我居然不會,查查找找發現這東西並不難,但是應該是比較常用的,畢竟json太好用了!我是直接用的jquery實現的,但是ajax也可以,不過我用的ajax的簡約版 getjson url,function 如下 function getsc...