te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>jquery - How to read json file located in project folder in iPhone PhoneGap using Javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to read json file located in project folder in iPhone PhoneGap using Javascript - Stack Overflow

programmeradmin3浏览0评论

I have to read a JSON file from folder that is located in a project.

I am using the following code:

var obj = "www/places.json";

How can I read a JSON file located in project folder www in iPhone PhoneGap using Javascript?

I have to read a JSON file from folder that is located in a project.

I am using the following code:

var obj = "www/places.json";

How can I read a JSON file located in project folder www in iPhone PhoneGap using Javascript?

Share Improve this question edited Aug 12, 2013 at 4:39 hharry asked May 4, 2013 at 16:12 hharryhharry 4121 gold badge9 silver badges21 bronze badges 1
  • Have look at stackoverflow./questions/14908717/… it has an example how to read a file and parse it as JSON. – Mattias Wadman Commented May 4, 2013 at 21:45
Add a ment  | 

2 Answers 2

Reset to default 15

You would read it just like it is on your server.

Solution 1 - jQuery

If jQuery usage is not a problem then use it like this:

//Load categories object JSON
jQuery.getJSON("categories.json", function(data){         
    // data is 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/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 you want to use only vanilla javascript then this is a solution for you

var xmlhttp;
var jsonObject;

// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
// code for 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 for IE7+, Firefox, Chrome, Opera, Safari
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        // code for 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"} 

I had same problem and got through with following snippet:

dojo.ready(function(){

   var xhrArgs = {
          url: "file:///Users/Desktop/configJSON.txt",
          handleAs: "json",
          load: function(data){

           targetNode.innerHTML = data;

                   // Your data from JSON
               alert("Name : "+data.fields[0].name+" Type :          "+data.fields[0].type+" Alias : "+data.fields[0].alias
               +" Editable : "+data.fields[0].editable);


      },
      error: function(error){
                      // targetNode.innerHTML = "An unexpected error occurred: " + error;
                      alert("An unexpected error occurred: " + error);
              }
              }

              // Call the asynchronous xhrGet
              var deferred = dojo.xhrGet(xhrArgs);
          });

      </script>
    </head>

    <body>

              <div id="licenseContainer"></div>
<body>              
发布评论

评论列表(0)

  1. 暂无评论