簡體   English   中英

語法錯誤,Ruby JSON字符串中出現意外的tIDENTIFIER

[英]Syntax error, unexpected tIDENTIFIER in Ruby JSON String

我正在嘗試將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,它看起來有效,因此我不確定出了什么問題。

將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中

赫雷多克

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM