我有一些麻煩逃脫和JSON數組上設置撇號。 我有這樣的:Escape和撇號設置JSON傳遞通過導軌到JS
@option_change = OptionChange.new(@product).to_json.html_safe
在我使用它在這樣的JS文件:
var json_test = $.parseJSON('<%= @option_change %>');
當這個js執行,這是回報:(這是我@option_change的內容)
'"[{"option":1,"properties":[{"sequence":null,"option_value":{"id":3,"value":"Test'WithError","image_uid":null,"image_name":null,"description":""},"product_sku_id":82,"available_stock":true}],"next_option":null}]"'
我因爲Test'WithError設置的撇號的錯誤,我嘗試使用GSUB。(「「」,「\\」」)像其他職位的建議,但我認爲我的問題需要一個更具體的要解決的方法。
@product:#<Product id: 48, name: "[ADENA][TESTE] 365b4ea2f838", resume: "365b4ea2f838", description: "<p>\r\n\t365b4ea2f838</p>\r\n", created_at: "2017-06-14 21:29:05", updated_at: "2017-06-20 13:10:58", category_id: 1, highlight: 0, highlight_image: nil, category_highlight: false, meta_description_deprecate: nil, meta_keywords_deprecate: nil, seo_title_deprecate: nil, custom_title_deprecate: nil, brand_id: nil, profile_property_id: nil, video_url: "", creditcard_split_config_id: nil, code: nil, product_type_id: 1, unpackable: false, ensemble: false, quantity_uses_decimal: false, minimum_to_buy: #<BigDecimal:7f93ee2f7f90,'0.1E1',9(18)>, delta: true, availability: true, visits: 73, additions: 0, removals: 0, custom_delivery_time_deprecated: nil, sales: nil, google_shopping_identifier_exists: true, private: false, keywords: "", extends_delivery_type: "minute", extends_delivery_time: nil>
你們有什麼想法如何?
謝謝!
嘗試gsub(「'」,「\\\''」) – gaetanoM
嘗試過它,在JSON內部,他認爲撇號作爲JSON文件的結尾而不是字符串的一部分。 –
'@ product'的價值是什麼? –