【问题标题】:Coffeescript json response processingCoffeescript json响应处理
【发布时间】:2016-11-05 22:37:17
【问题描述】:

我刚开始使用咖啡脚本。但是,我必须处理来自 rails 应用程序的 JSON 响应以生成谷歌地图标记。如何将JSON 数据解析到咖啡脚本中。

代码如下:

  $.ajax '/locations',
          type: 'GET'
          dataType: 'json'
          error: (jqXHR, textStatus, errorThrown) ->
              alert "error"
          success: (data, textStatus, jqXHR) ->
              buses = data

  window.initMap = ->
    map = new google.maps.Map(document.getElementById('map'),
      center:
        lat: 0.324680
        lng: 32.572633
      zoom: 11
    )
  if buses?
    for i in buses
      marker = new google.maps.Marker(position: new google.maps.LatLng(i["latitude"], i["longitude"])
      map: map
      title: i["number_plate"])
      marker.setMap(map)

  loadScript = ->
    script = document.createElement("script")
    script.type = "text/javascript"
    script.src = "https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxx&callback=initMap"
    document.getElementById('map_scripts').appendChild script
    return

  loadScript()

Json 响应看起来像这样{"locations":[{"number_plate":"UAW796N","terminal_number":2,"latitude":"0.4122","longitude":"33.235","speed":"46.0"}]}

【问题讨论】:

  • 使用JSON.parse,就像你写Javascript一样。

标签: javascript ruby-on-rails json coffeescript


【解决方案1】:

您可以使用__ 'locations.number_plate'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2020-05-14
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多