【问题标题】:Rails JSON parse unexpected token escapced charsRails JSON 解析意外的令牌转义字符
【发布时间】:2014-03-22 19:35:39
【问题描述】:

我有一个已转义的字符串,我想将其解析为 JSON,但使用 rails 时不断出错

{\"content\":{\"Soass__text_plain__abc_of_study\":\"Some of Study\",\"Dodeeedd\":
...........
........lots more of the string then comes some \u002d1 etc

第 160 行的解析错误: ...“id”:\u002d1

JSON::ParserError: 399: unexpected token at '{"spacer":"http://s.c.xxx.yyyy.com/scds/common/u/img/spacer.gif","i18n_get_discovered_upload":"\u003cstrong\u003eGet discovered\u003c/strong\u003e for your work! Add your videos, images, documents...","bg_promo_1":

我怎样才能 JSON 解析这个?

【问题讨论】:

  • 试试这个服务来定位你的 JSON 中的坏字符:jsonlint.com

标签: ruby-on-rails json parsing


【解决方案1】:

您可以使用散列或数组构建您的 json,它更简单干净

hash_of_values = {'foo' => 1, 'bar' => 2}
array_of_values = [hash_of_values]

然后:

JSON.parse(hash_of_values) => "{\"foo\":1,\"bar\":2}"
JSON.parse(array_of_values) => "[{\"foo\":1,\"bar\":2}]"

【讨论】:

  • 它来自上述来源,我需要得到这个“未解析”并且你解释的内容颠倒了
猜你喜欢
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 2014-02-20
  • 2015-08-28
  • 1970-01-01
相关资源
最近更新 更多