how do i initialize an array similar to what $.getJSON('getMessageDetails.php', function (json) will return
I need to set/initialize an array exactly to what JSON will look like so I can have an array that I can store old and new and compare if old = new
.
This is what I have:
function refresh()
{
var old_json = new Array();
var old_json["id"] = 0;
$.getJSON('getMessageDetails.php', function (json) {
var new_json["id"] = json[0].id
if (old_json != new_json)
{
$("#msg_id").html(json[0].id);
$("#subject").html(开发者_高级运维json[0].subject);
$("#unique_code").html(json[0].unique_code);
var old_json = json[0].id;
}
});
}
what I have at the top is not correct.
JSON returns:
["id":1,"subject":"freechat"...etc}]
try
var old_json = {};
instead of
var old_json = new Array();
This will initialise the old_json var as an object instead of an array
javascript array cannot not be associative.
try with "var old_json = new Object();"
you can use a simple variable too "var old_json = 0;"
moreover the assigniation at the end is wrong (var old_json = json[0].id;)
it should be like "old_json = json[0].id;" if you use a simple variable, and "old_json.id = json[0].id;" if old_json is an Object. (suppress the var there, as old_json is already declared)
精彩评论