Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

ruby on rails - What is a JSON octet and why are two required?

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

connection = ContextIO::Connection.new(key, secret)
messages = connection.all_messages(:account => account, :limit => 100, :since => (Time.now - 3000.day ))

The variable messages is formatted in JSON. Then I execute this:

 foo = JSON.parse(messages)['data']

Most of the time this works. Every now and again, I get this error message:

  A JSON text must at least contain two octets!  

That error message then refers to the line JSON.parse(messages)['data']

  1. What is an octet?

  2. Why must JSON text contain at least two octets?

  3. How do I prevent my code from breaking every time messages does not have two octets?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...