views:

377

answers:

1

I javascript I have an object that looks similar to:

var myObj = 
{
   prop1: 1,
   prop2: 2,
   prop3: ["a","b","c","d","e"],
   prop4: 4,
   prop5: ["f","g","h","i"]
}

It's an object containing a number of properties. Each property may or may not be an array.

  var serializedMyObj = JSON.stringify(myObj);

serializedMyObj is (found by viewing the results of the serialize function in firebug):

"{ "prop1":1, "prop2":2, "prop3":["a","b","c","d", "e"], "prop4":4, "prop5":["f","g","h","i"] }"

if I alert(serializedMyobj); it shows me:

{ "prop1":1, "prop2":2, "prop3":[], "prop4":4, "prop5":[] }

The real problem is when i pass this data into an Asp.Net PageMethod the server gets the same data I see when it's shown in the alert dialog, not in firebug. Somewhere it's losing the array values and only putting in [].

Does anyone know why this would happen or a way to fix it? It's probably something simple I'm overlooking.

+1  A: 

I get the following (correct) output on firefox:

{"prop1":1,"prop2":2,"prop3":["a","b","c","d","e"],"prop4":4,"prop5":["f","g","h","i"]}

What browser are you using?

Also, I noticed that myObj was lowercase in JSON.stringify(myobj); - I assume that was just a typo?

Justin Ethier
I'm also using firefox, how are you viewing the string after the serialization? (i fixed the typo, thanks)
John Boker
Via alert(serializedMyObj); ... But firebug also agrees with this output. Is this your actual output, or just sanitized for SO? Maybe there are some invalid characters in there somewhere???
Justin Ethier
This isnt the actual data, the actual data is integer properties with Guid arrays, the Guids are represented as strings. I's all pretty simple stuff, there must be something overlooked by me.
John Boker
You might want to carefully compare your data with the diagrams on http://json.org/ - perhaps you have an invalid character in there somewhere that is causing problems??
Justin Ethier
The problem was that i was filling the arrays in with an ajax call (that hadnt returned yet).
John Boker
Ah, that would do it. Anyway, I'm glad you tracked it down, and thanks for accepting my answer :)
Justin Ethier