Reputation: 321
I have a problem if i want to insert this json test into my phonegap page(which has jquery mobile included):
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>");
Then it doesn't work.. My question is, is JSON actually possible with phonegap+jquery mobile??
In the future i want a webservice which returns data in JSON format so that I can use this json data in my phonegap app.. I am now testing if json data works, by using hardcoded data..
Upvotes: 0
Views: 483
Reputation: 382150
It's probable this code is called after the page has finished loading.
So there is no point in using document.write.
Simply use the html function to fill what you want to fill (probably a div).
Note that you must wait for the DOM to be fully loaded. The best practice is to use the onload event handler and to put the script element at the end of the body.
<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>
Besides, a "JSON object" doesn't mean much as JSON is an exchange format. This is just a plain javascript object.
Upvotes: 1