What is a JSON octet and why are two required?

后端 未结 2 681
说谎
说谎 2020-12-05 22:32

I have incoming data, which I store in a variable messages:

connection = ContextIO::Connection.new(key, secret)
messages = connection.all_messag         


        
相关标签:
2条回答
  • 2020-12-05 23:25
    1. An octet is a group of 8 bits. Today, octet is synonymous with byte, but byte historically referred to any "native" grouping of bits, and that could mean 4,6,7, or 8 bits.
    2. JSON text must contain at least two octets because the top-level structure of a JSON document is an array or object, and the shortest representations of those are [] and {}, respectively.
    3. Check the value messages. It is probably empty, unset or consists of a single digit (like 4), which is not valid JSON, but accepted by many JSON implementations.
    0 讨论(0)
  • 2020-12-05 23:25

    While I totally agree with the other answer, in my case I had a valid JSON string but was still receiving the "JSON text must contain at least two octets" error message when trying to JSON.parse.

    My issue was that I was not specifying content type of application/json. Once I added that, JSON.parse worked without error.

    Hope this helps someone else.

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