var jsonstring ="";
function show01(fileinfo) {
//讀取json到字串jsonstring
file = fileinfo.files[0];
var fReader = new FileReader();
fReader.onload = function (event) {
jsonstring = event.target.result;
};
fReader.readAsText(file);
}
function initialjsonDoc()
{
jsonDoc = JSON.parse(jsonstring);
}
步驟1:建立next()函數來控制到下一筆
function next()
{
if(jsonDoc == undefined){initialjsonDoc();}
i++;
if(i>jsonDoc.length){i=0;}
changedata(i);
}
步驟2:建立previous()函數來控制到下一筆
function previous()
{
if(jsonDoc == undefined){initialjsonDoc();}
i--;
if(i<0){i=jsonDoc.length-1;}
changedata(i);
}
步驟3:建立顯示單筆資料的changedata(i)
var i=0;
var jsonDoc;
function changedata(i)
{
str ="書名:" + jsonDoc[i].title;
str +="<br/>作者:" + jsonDoc[i].author;
str +="<br/>類別:" + jsonDoc[i].category;
str +="<br/>出版日期:" + jsonDoc[i].pubdate;
str +="<br/>書號:" + jsonDoc[i].id;
document.getElementById("id01").innerHTML = str;
}
[
{
"title": "Dreamweaver CC 網站設計",
"author": "鄧文淵",
"category": "網頁設計",
"pubdate": "05/2014",
"id": "web001"
},
{
"title": "ASP.NET 3.5 網頁程式設計",
"author": "陳會安",
"category": "網頁設計",
"pubdate": "07/2014",
"id": "web002"
},
{
"title": "C#.NET 程式設計",
"author": "曹祖聖",
"category": "程式設計",
"pubdate": "11/2012",
"id": "prog001"
},
{
"title": "jQuery Mobile設計",
"author": "陳會安",
"category": "手機設計",
"pubdate": "06/2014",
"id": "mobile001"
}
]