0

Alright, I've got an interesting issue. My page makes a JSON request every once in a while. If the data hasn't changed, it doesn't need to do anything. The problem is, I can't figure out a way to prove if it has changed or not.

I tried something like this to no avail:

var stations = null;

function parseDynamicData(ard) {
    //this was something that gave me problems for days
    //nice little hack to fix it
    if (ard['details']['enabled'] == 'true' && ard['song']['art'] != 'undefined');
    {
        //turns off the 'server offline' page
        if (document.location.toString().indexOf('#offline') != -1)
            document.location = '/#tracks';
        //update UI
        $('#track-title').html(htmlDecode(ard['song']['title']));
        $('#track-artist').html(htmlDecode(ard['song']['artist']));
        $('#track-album').html(htmlDecode(ard['song']['album']));
        $('#track-art').attr('src', htmlDecode(ard['song']['art']));
        //set if it's play or pause visible
        if (htmlDecode(ard['details']['playing']) == 'true') {
            $('#control-pauseplay').html('Pause');
            $('#control-pauseplay').attr('href', '/track?proc=2');
        } else {
            $('#control-pauseplay').html('Play');
            $('#control-pauseplay').attr('href', '/track?proc=3');
        }
        //Now update all of the stations
        if (stations == null || stations != ard['stations']) {
            $.each(ard['stations'], function (key, value) {
                alert(key);
            });
            stations = ard['stations'];
        }
    }
}

Here is what a normal JSON response looks like:

{
    "song": {
        "art": "http://cont-sjl-1.pandora.com/images/public/amz/6/2/3/3/886978533326_500W_500H.jpg",
        "title": "Hold It Against Me",
        "artist": "Britney Spears",
        "album": "Femme Fatale Deluxe"
    },
    "details": {
        "playing": "true",
        "enabled": "true"
    },
    "stations": {
        "undefined": "False",
        "Alex Clare Radio": "False",
        "Rap Strength Training Radio": "False",
        "Drops Of Jupiter Radio": "False",
        "Tween Radio": "False",
        "Today's Hits Radio": "True"
    }
}

No matter what I do, however, it still goes through the JSON making an alert for every key. Does anybody know what I should do?

Freesnöw
  • 30,619
  • 30
  • 89
  • 138
  • I think some of your irrelevant code would be helpful for context... Off the top of my head, either stations is null (being overwritten elsewhere -- but that's easy to catch) or the `!=` comparator isn't doing what you think it's doing. Try http://stackoverflow.com/questions/3115982/how-to-check-javascript-array-equals or http://stackoverflow.com/questions/1068834/object-comparison-in-javascript – Patrick M Jun 11 '12 at 14:20
  • can you post the structure of your javascript object and JSON data? (i see no arrays in this code) – jbabey Jun 11 '12 at 14:20
  • I suspect that `stations` and `ard['stations']` are different arrays that merely have the same values. – apsillers Jun 11 '12 at 14:24
  • @apsillers if they have string keys they are objects, not arrays. – jbabey Jun 11 '12 at 14:25
  • @jbabey Oh, true enough; I didn't have my critical thinking hat on. Revised: they are different objects that merely have same key-value pairs. – apsillers Jun 11 '12 at 14:30
  • I did an update to my post. I added some more relevant information to help clear things up. – Freesnöw Jun 11 '12 at 15:29

2 Answers2

1

You could try using JSON.Stringify? Obviously what you get back from the JSON request is JSON, and I assume that you've converted the last request into an actual Object. So just call:

JSON.Stringify(Object) === JSONresult

and compare the two strings.

phenomnomnominal
  • 5,427
  • 1
  • 30
  • 48
  • 1
    I wouldn't do that unless we can guarantee that the server-side JSON is formatted exactly the same as the stringified client-side JSON (e.g., same key ordering, no differing whitespace, etc). – apsillers Jun 11 '12 at 14:36
1

I suspect that stations and ard['stations'] are different objects that are identical in structure. If you are rebuilding ard after each JSON request while stations holds the array from the previous ard, then the two objects are not the same object, so an exact match will fail, regardless whether the objects have identical key-value pairs.

What I would do is keep a exact string copy of the last JSON message and compare it with the current JSON string message before you parse it. Otherwise, you'll need to compare every single key-value pair of the current object against the prospective new one -- and if some of those values are objects themselves, things could get a little (needlessly) hairy.

If you can be certain that the server will always stringify a given object O the same way (and assming you don't need to worry about checking if an object on the client has mutated), then you can simply compare the JSON string versions of the object from the server.

apsillers
  • 112,806
  • 17
  • 235
  • 239