1

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}]
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
charlie_cat
  • 1,830
  • 7
  • 44
  • 73

2 Answers2

0

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

chim
  • 8,407
  • 3
  • 52
  • 60
  • ooo yes quite right..forgot sorry..so how can i now do: old_json["id":0]??? so that i can compare json[0].id with old_json["id"]? – charlie_cat Aug 31 '11 at 12:22
  • You should be able to compare the old_json object directly to the json[0] object. – chim Aug 31 '11 at 12:47
0

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)

roselan
  • 3,755
  • 1
  • 20
  • 20