使用jquery解析json檔案
阿新 • • 發佈:2019-01-24
有時需要使用jquery去解析對於本地的json檔案並使用。
假設有這樣一個json檔案:
{
"gift_list": [
{
"gift_id": "44",
"title": "愛神丘位元"
},
{
"gift_id": "43",
"title": "不孤單"
}
]
}
要解析這個json檔案,需要使用$.getJSON()方法。
$.getJSON('json檔案路徑/json_gift.json' ,function(dataJson){
$.each(dataJson.gift_list,function(i,infos){
if(infos.gift_id == 44) {
console.log(infos.title); //愛神丘位元
}else{
console.log(infos.title); //不孤單
}
}
})