Javascript JSON.stringify object containing arrays serialization problem
-
21-09-2019 - |
Question
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.
Solution
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?