有时需要使用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); //不孤单
}
}
})