我的表中的一個欄位具有 JSON 型別,因此在模型內部要保存字串,我需要執行以下操作:
create(modifications: string.to_json,
...
)
在視圖上顯示此modifications列由 JSON.parse 決議,這給了我錯誤784: unexpected token at '"test registrant history"'
長話短說:
2.4.5 :042 > json_test = 'testing the string'.to_json
=> "\"testing the string\""
2.4.5 :043 > JSON.parse(json_test)
JSON::ParserError: 784: unexpected token at '"testing the string"'
為什么會這樣?那么如何從字串中生成 JSON 呢?
[編輯]
我在 Rails 5 和 Ruby 2.4.5 上
uj5u.com熱心網友回復:
這是 json gem (<2) 版本的問題。為避免這種情況,您應該使用 json >2 的版本。因此,您可能會嘗試升級您的 rails 版本或僅使用quirks_mode: true.
JSON.parse(json_test, quirks_mode: true)
這是github json問題上的問題的鏈接
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411413.html
標籤:
上一篇:從Ruby中的哈希陣列中選擇欄位
下一篇:從json檔案中獲取所有多邊形
