windows 8 metro app 读取本地json的方法
2012-01-31 16:46
471 查看
metro app 读取本地json的方法
// read the file you want
function ReadAllDataFile(fileNameInLocalTree) {
var package = Windows.ApplicationModel.Package.current;
var installedLocation = package.installedLocation;
installedLocation.createFileAsync(fileNameInLocalTree, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
var size = stream.size;
if (size == 0) {
// Data not found
}
else {
var inputStream = stream.getInputStreamAt(0);
var reader = new Windows.Storage.Streams.DataReader(inputStream);
reader.loadAsync(size).then(function () {
//var contents = reader.readString(size); // fails with multibyte error if bad data (see legislators.getList.json)
// allocate the full array so readBytes can insert it in full
var array = new Array(size);
reader.readBytes(array);
var newString = "";
for (var i = 0; i < array.length; i++) {
// only printable characters (include spaces because could be part of names) (very rough here)
// http://www.csgnetwork.com/asciiset.html if (array[i] >= 32 && array[i] <= 126) {
var c = String.fromCharCode(array[i]);
newString += c;
}
}
var result = JSON.parse(newString);
var newYorkPopulation = result.NY.P001001;
// output to the screen
document.getElementById('outputhere').innerHTML = "New York Population: " + newYorkPopulation;
//outputhere.winControl.dataSource = postItems;
});
}
})
});
}
用法:
1.在js文件中调用函数:
(function () {
'use strict';
// Uncomment the following line to enable first chance exceptions.
Debug.enableFirstChanceException(true);
WinJS.Application.onmainwindowactivated = function (e) {
if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
// start of my insert code
ReadAllDataFile("\data\\states_data.json");
// end of my insert code
}
}
WinJS.Application.start();
})();
2.在html文件中
<h1>
<div id="outputhere" >
</div>
</h1>
// read the file you want
function ReadAllDataFile(fileNameInLocalTree) {
var package = Windows.ApplicationModel.Package.current;
var installedLocation = package.installedLocation;
installedLocation.createFileAsync(fileNameInLocalTree, Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFile) {
dataFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream) {
var size = stream.size;
if (size == 0) {
// Data not found
}
else {
var inputStream = stream.getInputStreamAt(0);
var reader = new Windows.Storage.Streams.DataReader(inputStream);
reader.loadAsync(size).then(function () {
//var contents = reader.readString(size); // fails with multibyte error if bad data (see legislators.getList.json)
// allocate the full array so readBytes can insert it in full
var array = new Array(size);
reader.readBytes(array);
var newString = "";
for (var i = 0; i < array.length; i++) {
// only printable characters (include spaces because could be part of names) (very rough here)
// http://www.csgnetwork.com/asciiset.html if (array[i] >= 32 && array[i] <= 126) {
var c = String.fromCharCode(array[i]);
newString += c;
}
}
var result = JSON.parse(newString);
var newYorkPopulation = result.NY.P001001;
// output to the screen
document.getElementById('outputhere').innerHTML = "New York Population: " + newYorkPopulation;
//outputhere.winControl.dataSource = postItems;
});
}
})
});
}
用法:
1.在js文件中调用函数:
(function () {
'use strict';
// Uncomment the following line to enable first chance exceptions.
Debug.enableFirstChanceException(true);
WinJS.Application.onmainwindowactivated = function (e) {
if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
// start of my insert code
ReadAllDataFile("\data\\states_data.json");
// end of my insert code
}
}
WinJS.Application.start();
})();
2.在html文件中
<h1>
<div id="outputhere" >
</div>
</h1>
相关文章推荐
- Android读取本地json文件的方法(解决显示乱码问题)
- windows 8 metro app 中 选中某个 item 弹出 appbar 的方法
- APP接口开发之PHP封装JSON数据 ,并读取JSON数据的方法
- $.get本地可以读取到json,服务器不能读取解决方法
- jQuery的getJSON方法不支持本地文件读取,一定要在服务器当中
- Java读取本地json文件及相应处理方法
- iOS Swift读取本地json文件报错的解决方法
- 在vue中读取本地Json文件的方法
- Android App中读取XML与JSON格式数据的基本方法示例
- Metro App/Windows 8 递归读取"我的音乐"目录下的所有音频文件
- 在桌面程序上和Metro/Moder/Windows store app的交互(相互打开,配置读取)
- nodejs读取本地中文json文件出现乱码解决方法
- Python txt文件读取写入字典的方法(json、eval)
- Flex结合JavaScript读取本地路径的方法
- 第一篇 Windows 8 开发Windows Metro style app环境配置
- Windows不能在本地计算机启动OracleDBConsoleorcl.错误代码2的解决方法
- 如何读取本地的json数据
- NATApp内网映射工具使用方法以及Tomcat自建本地web服务
- 京东云擎python wsgi windows下的本地测试方法
- 关于把本地应用封装成windows app发布审核通不过的问题