jquery - How to read json file located in project folder in iPhone PhoneGap using Javascript -
i have read json file folder located in project.
i using following code:
var obj = "www/places.json";
how can read json file located in project folder www
in iphone phonegap using javascript?
you read on server.
solution 1 - jquery
if jquery usage not problem use this:
//load categories object json jquery.getjson("categories.json", function(data){ // data yours parsed object });
example :
html :
<!doctype html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/> <script src="http://www.fajrunt.org/js/jquery-1.9.1.min.js"></script> <title>read json demo</title> <script> jquery.getjson("categories.json", function(data){ alert(data.balance); }); </script> </head> <body> read json demo </body> </html>
json file :
{"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
solution 2 - pure javascript
if want use vanilla javascript solution you
var xmlhttp; var jsonobject; // code ie7+, firefox, chrome, opera, safari if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } // code ie6, ie5 else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { jsonobject = json.parse(xmlhttp.responsetext); alert(jsonobject.balance); } } xmlhttp.open("get","categories.json",true); xmlhttp.send();
example :
html :
<!doctype html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/> <title>read json demo</title> <script> var xmlhttp; var jsonobject; // code ie7+, firefox, chrome, opera, safari if (window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } // code ie6, ie5 else { xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { jsonobject = json.parse(xmlhttp.responsetext); alert(jsonobject.balance); } } xmlhttp.open("get","categories.json",true); xmlhttp.send(); </script> </head> <body> read json demo </body> </html>
json file :
{"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
Comments
Post a Comment