phonegap +jquery mobile + JSON

phonegap +jquery mobile + JSON

本文关键字:JSON mobile +jquery phonegap      更新时间:2023-12-16

如果我想将这个json测试插入我的phonegap页面(其中包含jquery mobile),我会遇到问题:

var JSONObject = {  "name" : "Amit",
                 "address"  : "B-123 Bangalow",
                 "age"  : 23, 
                 "phone"   : "011-4565763",
                 "MobileNo"  : 0981100092
             };
             document.write("<h2><font color='blue'>Name</font>::"
                            +JSONObject.name+"</h2>");  
             document.write("<h2><font color='blue'>Address</font>::"
                            +JSONObject.address+"</h2>");  
             document.write("<h2><font color='blue'>Age</font>::"
                            +JSONObject.age+"</h2>");  
             document.write("<h2><font color='blue'>Phone No.</font>::"
                            +JSONObject.phone+"</h2>");  
             document.write("<h2><font color='blue'>Mobile No.</font>::"
                            +JSONObject.MobileNo+"</h2>");

那么它就不起作用了。。我的问题是,phonegap+jquery-mobile真的可以使用JSON吗??

将来我想要一个以JSON格式返回数据的Web服务,这样我就可以在我的phonegap应用程序中使用这个JSON数据。。我现在通过使用硬编码数据来测试json数据是否有效。。

很可能在页面加载完成后调用此代码。

因此,使用document.write.是没有意义的

只需使用html函数来填充您想要填充的内容(可能是div)。

请注意,您必须等待DOM被完全加载。最佳实践是使用onload事件处理程序,并将脚本元素放在正文的末尾。

<div id=idOfTheDivWhereYouWantToWrite></div>
<script>
$(document).ready(function() {
    var JSONObject = {  "name" : "Amit",
             "address"  : "B-123 Bangalow",
             "age"  : 23, 
             "phone"   : "011-4565763",
             "MobileNo"  : 0981100092
    };
    var html= "<h2><font color='blue'>Name</font>::"+JSONObject.name+"</h2>" 
    html += "<h2><font color='blue'>Address</font>::" +JSONObject.address+"</h2>";  
    html += "<h2><font color='blue'>Age</font>::" +JSONObject.age+"</h2>";  
    html += "<h2><font color='blue'>Phone No.</font>::"+JSONObject.phone+"</h2>";  
    html += "<h2><font color='blue'>Mobile No.</font>::"+JSONObject.MobileNo+"</h2>";
    $('#idOfTheDivWhereYouWantToWrite').html(html);
});
</script>

此外,"JSON对象"并不意味着什么,因为JSON是一种交换格式。这只是一个普通的javascript对象。