什么是JSON八位位组,为什么需要两个八位位组? [英] What is a JSON octet and why are two required?

查看:145
本文介绍了什么是JSON八位位组,为什么需要两个八位位组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有传入的数据,这些数据存储在变量messages中:

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 ))

变量messages的格式为JSON.然后我执行此操作:

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!  

然后该错误消息指向行JSON.parse(messages)['data']

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

  1. 什么是八位位组?

  1. What is an octet?

为什么JSON文本必须至少包含两个八位字节?

Why must JSON text contain at least two octets?

如何防止每次messages没有两个八位位组时代码都被破坏?

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

谢谢!

推荐答案

  1. 一个八位位组是一组8位.今天,八位位组与字节同义,但是字节在历史上是指任何本机"位组,这可能意味着4、6、7或8位.
  2. JSON文本必须至少包含两个八位字节,因为JSON文档的顶级结构是数组或对象,而它们的最短表示分别是[]{}.
  3. 检查值messages.它可能是空的,未设置的或由一位数字组成(例如4),这不是有效的JSON,但被许多JSON实现接受.
  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.

这篇关于什么是JSON八位位组,为什么需要两个八位位组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆