このようなかなりネストされた JS オブジェクトがあり、それを JSON エンコードする必要があるとします。
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"}
]}
]
};
ネイティブ ブラウザー (Chrome、Firefox、IE9/10 でテスト済み) を使用して JSON エンコードするとJSON.stringify
、次のような JSON 文字列が返されます (これが私の予想です)。
ネイティブ JSON.stringify JSFiddle の例
{
"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"
}
]
}
]
}
PrototypeJSまたはjson2.jsを使用しているページで同じことをしようとすると、奇妙なことが起こります。
その場合、JSON.stringify
同じオブジェクトで次の JSON が返されます。
ProtypeJS JSON.stringify JSFiddle の例
{
"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\"}]}]"
}
明らかに、最初に渡されたのと同じオブジェクトに JSON デコードされないため、上記は問題ですJSON.stringify
。
何が起こっているのか、そしてなぜこの違いがあるのか について誰でも詳しく説明できますか?
私は何が欠けていますか?