---
title: "后端传递一个json到script &quot;"
author: "hhg08"
date: "2017-06-01T16:53:47Z"
tags: []
categories: ["编程语言"]
slug: "json-script-quot"
draft: false
lastmod: "2017-06-01T16:53:47Z"
id: 2377385
url: "https://hhg08.iteye.com/blog/2377385"
---

@ss = {
      name: '31231',
      sex: 'male'
    }.to_json.html_safe
当后端传递的实例变量没有加上.html_safe时，看到如下或者直接报错时
{&quot;name&quot;:&quot;31231&quot;,&quot;sex&quot;:&quot;male&quot;}
在ruby后端可以加上.html_safe得到一个不带&quot之类的
{"name":"31231","sex":"male"}

script的单双引号是不能乱用<%= @ss%> 在script里使用单引号，不然认为是两个引号。

然后使用 $.parseJSON('<%= @ss %>')获得一个jequery对象，就可以直接调用里面的字段了