【问题标题】:Syntax error, unexpected tIDENTIFIER in Ruby JSON String语法错误,Ruby JSON 字符串中出现意外的 tIDENTIFIER
【发布时间】:2016-03-13 00:13:44
【问题描述】:

我正在尝试使用 RSpec 和 Capybara 以及包含 Transloadit 上传的 Rails 4 应用程序创建功能测试。

我正在尝试按照他们的gem 文档来终止对 Transloadit 的调用,但我遇到了一个错误。在文档中,它说要放置 JSON 值,如下所示:

def example_json
  "{ ... JSON content from a real POST ... }"
end

但是,当我放置我的示例 JSON 时:

def example_json
    "{
      "ok": "ASSEMBLY_COMPLETED",
      "http_code": 200,
      "message": "The assembly was successfully completed.",
      "assembly_id": "ac9daa70e7bc11e58cfdb3c26b8231f5",
      "parent_id": null,
      "account_id": "067d87c0a16f11e59fa6a78f6ec29a04",
      "template_id": "5494d070a37811e5adb70b0aa0a9dfa6",
      "instance": "mhairi.transloadit.com",
      "assembly_url": "http://api2.mairi.transloadit.com/assemblies/ac9daa70e7bc11e58cfdb3c26b8231f5",
      "assembly_ssl_url": "https://mairi.transloadit.com/assemblies/ac9daa70e7bc11e58cfdb3c26b8231f5",
      "bytes_received": 721,
      "bytes_expected": 721,
      "upload_duration": 0.037,
      "client_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
      "client_ip": "81.227.172.81",
      "client_referer": "http://localhost:3000/lessons/hello/edit",
      "start_date": "2016/03/11 19:08:44 GMT",
      "is_infinite": false,
      "has_dupe_jobs": false,
      "execution_start": "2016/03/11 19:08:44 GMT",
      "execution_duration": 0.012,
      "notify_start": null,
      "notify_url": null,
      "notify_status": null,
      "notify_response_code": null,
      "notify_duration": null,
      "last_job_completed": null,
      "fields": {},
      "running_jobs": [],
      "bytes_usage": 0,
      "executing_jobs": [],
      "started_jobs": [],
      "parent_assembly_status": null,
      "params": "{\"template_id\":\"187d070a37811e5adb70b0aa0a9dfa6\",\"auth\":{\"key\":\"****\",\"expires\":\"2016/03/11 19:38:25+00:00\"}}",
      "template": "{\"steps\":{\"safe\":{\"use\":\":original\",\"robot\":\"/file/virusscan\",\"error_on_decline\":true},\"image_thumbs\":{\"use\":\"safe\",\"robot\":\"/image/resize\",\"result\":true,\"format\":\"png\",\"width\":200,\"height\":200,\"resize_strategy\":\"crop\"},\"optimized\":{\"use\":\"image_thumbs\",\"robot\":\"/image/optimize\",\"result\":true},\"files\":{\"use\":\"safe\",\"robot\":\"/file/filter\",\"declines\":[[\"${file.meta.duration}\",\">\",\"1800\"]],\"error_on_decline\":true},\"encode\":{\"use\":\"files\",\"robot\":\"/video/encode\",\"ffmpeg_stack\":\"v2.2.3\",\"preset\":\"ipad\"},\"export\":{\"use\":[\"encode\",\"optimized\"],\"robot\":\"/s3/store\",\"key\":\"****\",\"secret\":\"****\",\"bucket\":\"foobar\"}}}",
      "uploads": [],
      "results": {}
    }"
  end

我收到错误syntax error, unexpected tIDENTIFIER

我检查了 JSON here,它看起来有效,所以我不确定出了什么问题。

【问题讨论】:

  • 您在 "-quoted 字符串中使用 "。也许您可以将字符串括在 '.

标签: ruby-on-rails ruby json ruby-on-rails-4


【解决方案1】:

将 JSON 字符串中的所有双引号替换为单引号。

您的双引号字符串文字不能包含不转义的双引号。 "John said, \"Well ain't that something.\""

您可以在双引号内使用单引号,无需转义。

单引号(无效的 JSON)

def example_json
  "{
    'ok': 'ASSEMBLY_COMPLETED',
    'http_code': 200
  }"
end

另一个 StackOverflow 成员指出单引号在 JSON 对象中无效。要获得有效的 JSON,您可以使用转义的双引号。

转义双引号

def example_json
  "{
    \"ok\": \"ASSEMBLY_COMPLETED\",
    \"http_code\": 200
  }"
end

由于在整个 JSON 对象中输入反斜杠会相当痛苦,因此将有效的 JSON 包装在多行 heredoc 中。

Heredoc

def example_json
  <<-JSON
  {
    "ok": "ASSEMBLY_COMPLETED",
    "http_code": 200
  }
  JSON
end

http://rubyquicktips.com/post/4438542511/heredoc-and-indent

【讨论】:

  • JSON 字符串和值需要用双引号括起来 -json.org - 所以您的示例在技术上无效
  • @TomWalpole 添加了关于无效 JSON 的注释,并提供了两种具有有效 JSON 的替代方法,包括 heredoc。感谢您指出这一点!
猜你喜欢
  • 2014-09-16
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多