2016-04-11 5 views
0

Как получить ответ json в переменной javascript?Как сохранить json-ответ на переменную javascript и отображение

Это мой файл index.json.jbuilder

`json.array! @properties do |p| 
 
    json.id p.id 
 
    json.Space_name p.Space_name 
 
    json.address p.address 
 
    json.city p.city 
 
    json.state p.state 
 
    json.country p.country 
 
    json.latitude p.latitude 
 
    json.longitude p.longitude 
 
end`

Теперь я получаю ответ JSON, как этот

[{"id":22,"Space_name":"mumbai","address":"mumbai","city":"sharjah","state":"fujairah","country":"United Arab Emirates","latitude":"19.0759837","longitude":"72.87765590000004"},{"id":2,"Space_name":"Bangalore","address":"Banglore","city":"abu-dhabi","state":"ajman","country":"United Arab Emirates","latitude":"37.2809455","longitude":"49.59241339999994"}

, но мне нужно вывод, как это (хранится в одной переменной), для , например:

properties= [{"id":22,"Space_name":"mumbai","address":"mumbai","city":"sharjah","state":"fujairah","country":"United Arab Emirates","latitude":"19.0759837","longitude":"72.87765590000004"},{"id":2,"Space_name":"Bangalore","address":"Banglore","city":"abu-dhabi","state":"ajman","country":"United Arab Emirates","latitude":"37.2809455","longitude":"49.59241339999994"}

Здесь я использую JBuilder камень для JSon откоса. Любая помощь примечательна.

ответ

1

Не работал с jbuilder, но я полагаю, что это должно сработать.

#In controller 

@properties_json = 'json.array! @properties do |p| 
    json.id p.id 
    json.Space_name p.Space_name 
    json.address p.address 
    json.city p.city 
    json.state p.state 
    json.country p.country 
    json.latitude p.latitude 
    json.longitude p.longitude 
end' 

#In the View(*.html) 

<script type="text/javascript"> 
    var properties = <%= @properties_json %>; 
</script> 
Смежные вопросы