Ruby-on-rails 一个 JSON 文本必须至少包含两个八位字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8390256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:26:52  来源:igfitidea点击:

A JSON text must at least contain two octets

ruby-on-railsrubyjsonirb

提问by Cory

I received this error, and I couldn't find any reasonable answer to this question, so I thought I'd write a summary of the problem.

我收到这个错误,我找不到这个问题的任何合理答案,所以我想我会写一个问题的摘要。

If you run this snippet in irb:

如果您在 irb 中运行此代码段:

JSON.parse( nil )

You'll see the following error:

您将看到以下错误:

TypeError: can't convert nil into String

I was kind of expecting the function to return nil, and not a TypeError. If you convert all input using to_s, then you'll see the octet error:

我有点期待函数返回nil,而不是TypeError. 如果使用 转换所有输入to_s,则会看到八位字节错误:

JSON::ParserError: A JSON text must at least contain two octets!

That's just fine and well. If you don't know what an octet is, read this post for a summary and solution: What is a JSON octet and why are two required?

那很好。如果您不知道什么是八位字节,请阅读这篇文章以获取摘要和解决方案: 什么是 JSON 八位字节以及为什么需要两个?

Solution

解决方案

The variable you're passing in is an empty string. Don't attempt to use an empty string in the JSON.parsemethod.

您传入的变量是一个空字符串。不要尝试在JSON.parse方法中使用空字符串。

Question

So, now I know the cause of the error, what pattern should I use to handle this? I'm a bit loathe to monkey patch the JSON library to allow nilvalues. Any suggestions would be greatly appreciated.

所以,现在我知道了错误的原因,我应该使用什么模式来处理这个问题?我有点讨厌修补 JSON 库以允许nil值。任何建议将不胜感激。

采纳答案by Alex Wayne

parsed = json && json.length >= 2 ? JSON.parse(json) : nil

But really the library should be able to handle this case and return nil. Web browsers with built-in JSON support seem to work just like you expect after all.

但实际上图书馆应该能够处理这种情况并返回 nil。毕竟,具有内置 JSON 支持的 Web 浏览器似乎就像您期望的那样工作。



Or to do it with a only slightly intrusive mini patch:

或者用一个稍微侵入性的迷你补丁来做到这一点:

module JSON
  def self.parse_nil(json)
    JSON.parse(json) if json && json.length >= 2
  end
end

parsed = JSON.parse_nil(json)

回答by Денис Препелица

data.presence && JSON.parse(data)

JSON.parse(data.presence || '{}')

回答by Sagar Ranglani

According to json.org

根据json.org

JSON is built on two structures:

JSON 建立在两种结构上:

  1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.

  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

  1. 名称/值对的集合。在各种语言中,这被实现为对象、记录、结构、字典、哈希表、键控列表或关联数组。

  2. 值的有序列表。在大多数语言中,这被实现为数组、向量、列表或序列。

So, minimum two octets(8 bits) required at the top level would be {}or []

因此,顶层所需的最少两个八位字节(8 位)将是{}[]

IMO, the best solution would be to make sure the argument to JSON.parseis either an strigified object or a strigified array. :-)

IMO,最好的解决方案是确保参数 toJSON.parse是一个严格的对象或一个严格的数组。:-)

回答by Ich

hash = JSON.parse(json) rescue {}
array = JSON.parse(json) rescue []
string = JSON.parse(json) rescue ''