【问题标题】:JSON.parse error 785 unexpected token errorJSON.parse 错误 785 意外令牌错误
【发布时间】:2019-08-26 05:53:32
【问题描述】:

我正在尝试使用 JSON.parse 在我的 Rails 5 应用程序中解析 JSON 数据。我不断收到此错误:

"#<JSON::ParserError: 785: unexpected token at ': Hours,\n  \"config\": {\n    \"blocks\": <\n      {\n        \"type\": table,\n        \"name\": Hours from Users,\n        \"fields\": [\n          \"customer\",\n          \"project\",\n          \"user\",\n          \"task\",\n          \"hour_type\",\n          \"amount\"\n        ]\n      }\n    >\n  }>"

【问题讨论】:

  • 以 ': Hours' 开头的不是合法的 json。
  • 你有什么问题?
  • 我的问题是如何解决这个错误?
  • 该字符串不包含有效的 JSON,因此无法解析。谁来解决?取决于你从哪里得到字符串?你建了吗?这修复了您构建 JSON 和构建有效 JSON 的方式。有人给你发了那个字符串吗?然后询问该人该字符串是否实际上是 JSON 或其他内容。

标签: json ruby


【解决方案1】:

如果您打印该消息,则如下所示:

#<JSON::ParserError: 785: unexpected token at ': Hours,
  "config": {
    "blocks": <
      {
        "type": table,
        "name": Hours from Users,
        "fields": [
          "customer",
          "project",
          "user",
          "task",
          "hour_type",
          "amount"
        ]
      }
    >
  }>

您忘记引用某些值,例如 tableHours from Users,它们需要双引号 " 才能成为有效的 json。

(也不确定额外的&lt;&gt;,可能来自解析器错误)

【讨论】:

    【解决方案2】:

    :json 更改为:hybrid 喜欢:

    Rails.application.config.action_dispatch.cookies_serializer = :hybrid
    

    解释in this Rails issue:

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 2018-02-11
      • 2011-07-05
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多