1

How come this:

data-something='{property:1}'

doesnt work, but this

data-something='{"property":1}'

does. Does it

4

1 回答 1

5

Because JSON requires that object keys be quoted. This is mandated by the JSON specification; accepting non-quoted keys would mean that the implementation is not JSON-compliant.

This decision was made to side-step the issue of reserved keywords in JavaScript. It was desired that valid JSON also be a valid JavaScript expression, and to make that happen you would have to quote keys like return and function. To simplify specification of the JSON language, it was decided to require that all keys be quoted rather than maintain a list of keys that must be quoted and thereby complicate the JSON grammar (as well as tie the JSON language more closely to the JavaScript language than would otherwise be required).

于 2013-03-04T22:03:02.437 回答