views:

382

answers:

1

I have this object which I use as a list of objects:

var objList = new Object();

This is then serialized using JSON serialize. If there are no object added or all objects have been removed from the list and the blank objList is serialized, parsing the objList using JSON parser in IE, it will occasionally fail to evaluate the objList as a JavaScript object.

This causes the Object doesn't support this property or method error when tying to add an object to the objList:

objList['idx']=objData;

Does anyone know why does IE occasionally fail to evaluate objList:{} to an object and has someone else come across this issue.

The actual JSON string when the objList is '{}'

The objList is initialised:

objList = g_objList.parseJSON();
A: 

your serializer isn't working right if the json string is g_objList='{}' I would suggest doing something like...

objList = objList || {};

before using objList. I'm guessing you are getting a g_objList object string instead of the empty objlist. the above line after your eval/load of the json should clear things up.

Essentially it will set objList to your existing variable, or create a new, empty, object.

Tracker1
Basically the JSON string is held in g_objList which is saved on the clients machine. When needed the objList is initialised with value of g_objList: objList = g_objList.parseJSON(); I've got checks in place that detect the exception and if the JSON string = '{}' I initialise objList to objList = new Object(); What I really don't understand is why it works most of the times when the JSON is set to '{}' but sometimes the IE fails to evaluate the '{}'. The solution is used by 1000 users each day and the code objList = g_objList.parseJSON(); fails 3 or 5 times a day when JSON string set to '{}'.
bgosalci
Is the url being requested *really*returning '{}', or is it returning an error? just the same... objlist = g_onjList.parseJSON() || {}; should work.. forcing the ocject. Also, what JSON parser are you using? (json.org, asp.net ajax, prototype.js)?
Tracker1