如何在Ruby中解析JSON字符串数据,其中键是符号而不是字符串?

huangapple go评论94阅读模式
英文:

How to parse json string data having symbols as key instead of string in Ruby?

问题

这是 JSON 字符串的示例:

json = "{\"status\":\"ok\", \"hitcount\":\"1\", \"request_date\":\"Wed, 10 Oct 2019 00:00:00 +0000\"}"

我已经尝试了下面提到的代码片段,但这没有起作用并且报错 JSON::ParserError (765: unexpected token at

require 'json'
JSON.parse(json)
英文:

Here's the json string example

json = "{:status=>\"ok\", :hitcount=>\"1\", :request_date=>Wed, 10 Oct 2019 00:00:00 +0000}"

I have tried below mentioned snippet but this didn't work and gives error JSON::ParserError (765: unexpected token at

require 'json'
JSON.parse(json)

答案1

得分: 0

抱歉,JSON不能表示Ruby符号。JSON还要求键和值都要用引号括起来,所以您提供的内容不是有效的JSON,会导致JSON解析器出错。您只需使用以下内容:

json = "{\"status\": \"ok\", \"hitcount\": \"1\", \"request_date\": \"Wed, 10 Oct 2019 00:00:00 +0000\"}"

然后Ruby JSON解析器(感谢@engineersmnky!)具有一个巧妙的功能:

JSON.parse(json, symbolize_name: true)
英文:

Unfortunately, JSON can't represent Ruby symbols. JSON also requires both keys and values to be quoted, so what you've provided isn't valid JSON and will break a JSON parser. You want just:

json = "{\"status\": \"ok\", \"hitcount\": \"1\", \"request_date\": \"Wed, 10 Oct 2019 00:00:00 +0000\"}"

And then the Ruby JSON parser (thanks @engineersmnky!) has a nifty feature:

JSON.parse(json, symbolize_name: true)

答案2

得分: 0

这似乎是一个格式问题。例如,如果您有一个普通的哈希表,其中包含相同的数据,并将其转换为JSON,它将看起来有些不同:

require 'json'
# 帖子中的原始值
# json = "{:status=>\"ok\", :hitcount=>\"1\", :request_date=>Wed, 10 Oct 2019 00:00:00 +0000}"

hash = {:status=>"ok", :hitcount=>"1", :request_date=>"Wed, 10 Oct 2019 00:00:00 +0000"}
json = hash.to_json
puts json  # {"status":"ok","hitcount":"1","request_date":"Wed, 10 Oct 2019 00:00:00 +0000"}

parsed = JSON.parse(json)
puts parsed  # {"status"=>"ok", "hitcount"=>"1", "request_date"=>"Wed, 10 Oct 2019 00:00:00 +0000"}

您可以在这里看到我所指的问题:https://onlinegdb.com/tr6JVAV6y

所以我猜您需要查看JSON数据的来源,并确保以正确的格式发送它。

英文:

It does look like a formatting issue. For example if you were to have an ordinary hash with the same data, and convert it to JSON it will look a bit different:

require 'json'
# Original value in post
# json = "{:status=>\"ok\", :hitcount=>\"1\", :request_date=>Wed, 10 Oct 2019 00:00:00 +0000}"

hash = {:status=>"ok", :hitcount=>"1", :request_date=>"Wed, 10 Oct 2019 00:00:00 +0000"}
json = hash.to_json
puts json  # {"status":"ok","hitcount":"1","request_date":"Wed, 10 Oct 2019 00:00:00 +0000"}


parsed = JSON.parse(json)
puts parsed  # {"status"=>"ok", "hitcount"=>"1", "request_date"=>"Wed, 10 Oct 2019 00:00:00 +0000"}

You can see what I mean here: https://onlinegdb.com/tr6JVAV6y

So I'd guess you'll need to see where that JSON is coming from and have it sent in a proper format.

答案3

得分: 0

你可以使用eval()来将这个字符串转化为Ruby代码(一个哈希)。你可能会遇到一些问题,因为日期没有用引号括起来(我只是让ChatGPT为其编写正则表达式)。这将把你的字符串转化为一个合适的Ruby哈希:

json = "{:status=>\"ok\", :hitcount=>\"1\", :request_date=>Wed, 10 Oct 2019 00:00:00 +0000}"
fixed_string = json.gsub(/:request_date=>(\w+,\s\d+\s\w+\s\d+\s\d+:\d+:\d+\s\+\d+)/, ':request_date=>"\"')
hash = eval(fixed_string)

输出:

{:status=>"ok", :hitcount=>"1", :request_date=>"Wed, 10 Oct 2019 00:00:00 +0000"}

请注意,如果无法控制输入,使用eval()存在安全风险,问题似乎出在输出如何生成上。

英文:

You can use eval() to turn this string into ruby code (a hash). You will get some issues because the date is not quoted. (I just asked ChatGPT to write the Regex for it). This will turn your string into a proper ruby hash:

json = "{:status=>\"ok\", :hitcount=>\"1\", :request_date=>Wed, 10 Oct 2019 00:00:00 +0000}" 

fixed_string = json.gsub(/:request_date=>(\w+,\s\d+\s\w+\s\d+\s\d+:\d+:\d+\s\+\d+)/, ':request_date=>""')

hash = eval(fixed_string)

Output:

{:status=>"ok", :hitcount=>"1", :request_date=>"Wed, 10 Oct 2019 00:00:00 +0000"}

Please note that using eval comes with security risks if you cannot control the input and the problem seems to be how your output is generated.

huangapple
  • 本文由 发表于 2023年7月28日 01:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定