Let's say I have a fairly nested JS object like this and I need to JSON-encode it:
var foo = {
"totA": -1,
"totB": -1,
"totC": "13,052.00",
"totHours": 154,
"groups": [
{"id": 1,
"name": "Name A",
"billingCodes": [
{"bc": "25", "type": "hours", "hours": "5", "amount": "$25.00"}
]}
]
};
If I JSON-encode it using the native browser JSON.stringify
(tested in Chrome, Firefox, IE9/10), I get back a JSON string that looks like this (which is what I expect):
Native JSON.stringify JSFiddle example
{
"totA": -1,
"totB": -1,
"totC": "13,052.00",
"totHours": 154,
"groups": [
{
"id": 1,
"name": "Name A",
"billingCodes": [
{
"bc": "25",
"type": "hours",
"hours": "5",
"amount": "$25.00"
}
]
}
]
}
The weirdness comes in if I try to do the same thing on a page that's using either PrototypeJS or json2.js.
In that case, JSON.stringify
on the same object gives me back the following JSON:
ProtypeJS JSON.stringify JSFiddle example
{
"totA": -1,
"totB": -1,
"totC": "13,052.00",
"totHours": 154,
"groups": "[{\"id\": 1, \"name\": \"Name A\", \"billingCodes\": [{\"bc\": \"25\", \"type\": \"hours\", \"hours\": \"5\", \"amount\": \"$25.00\"}]}]"
}
Obviously, the above is a problem because it doesn't JSON-decode to the same object that was originally passed to JSON.stringify
.
Can anyone elaborate on what's going on and why there's this difference?
What am I missing?