Ruby JSON解析器在不喜欢空格时是合理的吗?

时间:2011-01-20 01:49:23

标签: ruby json

我正在使用json_pure-1.4.6并且无法解析Chrome的SpeedTracer中的数据。特别是它失败了:

705: unexpected token at '{"type":2147483642,"time":8872.551025390625,"data":{"identifier":1,"time":1295485209.698246,"request":{"url":"http://localhost:3000/login/new","httpMethod":"GET","httpHeaderFields":{"User-Agent":"Mozilla/5.0'

这显然是一个不完整的陈述,但在文件本身看起来像这样:

{"type":2147483642,"time":8872.551025390625,"data":{"identifier":1,"time":1295485209.698246,"request":{"url":"http://localhost:3000/login/new","httpMethod":"GET","httpHeaderFields":{"User-Agent":"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.634.0 Safari/534.16","Accept":"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"}},"redirectResponse":{"isNull":true}},"sequence":116}

我尝试通过JSONLint运行它并没有抱怨:

{
    "type": 2147483642,
    "time": 8872.551025390625,
    "data": {
        "identifier": 1,
        "time": 1295485209.698246,
        "request": {
            "url": "http://localhost:3000/login/new",
            "httpMethod": "GET",
            "httpHeaderFields": {
                "User-Agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.634.0 Safari/534.16",
                "Accept": "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
            }
        },
        "redirectResponse": {
            "isNull": true
        }
    },
    "sequence": 116
}

由于它在空白处失败,我尝试删除“User-Agent”行中的所有空格,然后json_pure然后解析它而没有错误。这是一个json_pure错误吗?

1 个答案:

答案 0 :(得分:4)

我只是将JSON字符串粘贴到文件中,然后运行:

require 'rubygems'
require 'json/pure'
str = File.open('pasted.json').read
x = JSON.parse(str)

...并获得了没有错误的预期值。

ruby -v
  => ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin9.8.0]
gem list json_pure
  => json_pure (1.4.6)

显然,您的设置有些古怪,但我不知道它是什么。