2

I have a JSON string like this one: (data is faked, my company is very restrictive...)

{
    "people" : {
        "101" : {
            "name" : "Strub, Thomas Reto",
            "birthdate" : "/Date(690595200000)/"
        }
    },
    "generated" : "/Date(1340755200000)/"
}

and I use this reviver when parsing:

var jsonMSDateTime = new RegExp('^\\/Date\\([0-9]{13}\\)\\/$');
function jsonReviver(key, value) {
    if (jsonMSDateTime.test(value)) return new Date(value.substr(6, 13) * 1);
    else return value;
}

My Problem is the following: If I run this script in IE, the generated date is parsed, but the birthdate is not. (It works in chrome, however.)

What mistake did I make?

6
  • What is the surrounding code? How is the reviver applied to the JSON? Chances are the birthdate field is never even processed (due to IE erroring out elsewhere). Open the console (F12), and add console.log('Reviving ' + key + ' ' + value); to the beginning of the function to see if the birthdate is ever passed to the reviver.
    – Supr
    Jun 27, 2012 at 11:45
  • Only in IE? Any errors? Can you reproduce this in jsFiddle? Use console for debugging (remember that under IE you have to actually open the console before calling it, otherwise it will throw exception).
    – freakish
    Jun 27, 2012 at 11:45
  • @Supr you are right, the birthday is never processed, instead an item with the key 0 and the value undefined is processed. Do I have to return a different value instead? (The item is not removed, as undefined should cause, instead it's just not processed. If I change the peoples-object to an array ("people" : [{ ... }]), everything works fine.
    – StrubT
    Jun 27, 2012 at 11:53
  • If making people and array instead suits the model (seems like it would) then that's what I would do. Sounds like the code that is applying the reviver has problems dealing with composed objects though, since it is passing undefined instead of the person.
    – Supr
    Jun 27, 2012 at 12:03
  • @Supr Okay, but then I have to loop through all people to find the one I need, if I have an object with the id as key, I can directly access the needed person. Thank you for the answer very much. However, if there is a solution with which i can keep my JSON, i'd be much happier.
    – StrubT
    Jun 27, 2012 at 12:07

1 Answer 1

2

This is definetly a bug in IE (at least in IE9). consider this example:

var js = {
    "test" : "test",
    "test2" : {
        "a" : "1"
    }, 
    "1" : { 
        "b" : "2" 
    }
};
var json = JSON.stringify(js); /* <---- all fine here */

function jsonReviver(key, value) {
    return value;
}

var jsonParsed = JSON.parse(json, jsonReviver);
jsonParsed["1"] /* <---- undefined */

The thing is, that if you change the key "1" to a nonnumeric one (for example "a1") it works. So it seems that the parser is not dealing well with numeric keys (don't know why). Numeric values are fine though.

Note that even if you use JSON.parse(json); the result will still be incorrect. I've tested it with jQuery's $.parseJSON and it is not working as well.

So there are (at least) three solutions to your problem:

  1. Stop supporting IE. :)
  2. Use JavaScript's eval("("+json+")");. After all JSON is a valid JavaScript. I wouldn't advice it though, this might be dangerous.
  3. Change keys in JSON so that they are never numerical.
1
  • I'd love to stop supporting IE, unfortunately, it's the company's only allowed browser... I'll take the third solution, thank you very much for your fast response.
    – StrubT
    Jun 27, 2012 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.