JSON.stringify() to UTF-8

前端 未结 1 1176
生来不讨喜
生来不讨喜 2021-02-15 19:34

Javascript uses as far as I know UTF-16 fundamentally as a standard for strings. With JSON.stringify() I can create a JSON string from an object.

Is that JSON st

1条回答
  •  旧巷少年郎
    2021-02-15 20:09

    JavaScript engines are allowed to use either UCS-2 or UTF-16.

    So, yes, JSON.stringify() will return a string in whatever encoding your implementation uses for strings. If you were to find a way to change that encoding within the context of your script, it would no longer be a valid JavaScript string.

    For serialising it over a network, though, I would expect it to automatically be transcoded into the character set of the HTTP request (assuming you're talking about HTTP). So if you send it via HTTP POST with a character set of UTF-8, your browser should transparently handle the transcoding of that data before it is sent.

    Otherwise browsers would really struggle with character set handling.

    0 讨论(0)
提交回复
热议问题