gpt4 book ai didi

javascript - 如何从 rails @variable 访问 JSON?

转载 作者:行者123 更新时间:2023-11-29 22:17:52 28 4
gpt4 key购买 nike

在 Rails 中,我调用了一个返回 JSON 的 API:

{"observations":{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","observation_start":"1776-07-04","observation_end":"9999-12-31","units":"pca","output_type":"1","file_type":"xml","order_by":"observation_date","sort_order":"asc","count":"263","offset":"0","limit":"100000","observation":[{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"1947-01-01","value":"."},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"1947-04-01","value":"-0.60853"}

在 Controller 中,我将 JSON 传递给实例变量:

# Send the http request
response = Net::HTTP.get_response(uri)


# captures the http response
test = response.body

#converts the http response hash from xml to json
show_info = Hash.from_xml(test).to_json
@it = show_info

最后我在 View 中渲染了实例变量:

<script>
x = "<%= @it %>"
</script>

如果我在控制台输入 x enter,弹出的 json 格式有点不同,这可能是我的问题。

 "{&quot;observations&quot;:{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;observation_start&quot;:&quot;1776-07-04&quot;,&quot;observation_end&quot;:&quot;9999-12-31&quot;,&quot;units&quot;:&quot;pca&quot;,&quot;output_type&quot;:&quot;1&quot;,&quot;file_type&quot;:&quot;xml&quot;,&quot;order_by&quot;:&quot;observation_date&quot;,&quot;sort_order&quot;:&quot;asc&quot;,&quot;count&quot;:&quot;263&quot;,&quot;offset&quot;:&quot;0&quot;,&quot;limit&quot;:&quot;100000&quot;,&quot;observation&quot;:[{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2010-10-01&quot;,&quot;value&quot;:&quot;2.39307&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2011-01-01&quot;,&quot;value&quot;:&quot;0.07892&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2011-04-01&quot;,&quot;value&quot;:&quot;2.47721&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2011-07-01&quot;,&quot;value&quot;:&quot;1.27864&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2011-10-01&quot;,&quot;value&quot;:&quot;4.09234&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2012-01-01&quot;,&quot;value&quot;:&quot;1.96053&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2012-04-01&quot;,&quot;value&quot;:&quot;1.25266&quot;},{&quot;realtime_start&quot;:&quot;2012-12-27&quot;,&quot;realtime_end&quot;:&quot;2012-12-27&quot;,&quot;date&quot;:&quot;2012-07-01&quot;,&quot;value&quot;:&quot;3.10599&quot;}]}}"

我的问题是如何访问其中一个键值对?我需要用javascript解析它吗?有没有办法通过点符号来做到这一点?

最佳答案

我依稀记得以前做过这样的事情。问题是我从 Web 服务获取一个 json 字符串,然后在其上调用 .to_json。

尝试改变这个:

Hash.from_xml(test).to_json

为此:

Hash.from_xml(test)

如果这不起作用,我认为另一篇文章应该可以帮助您正确格式化 json: Weird JSON Javascript problem in Rails

关于javascript - 如何从 rails @variable 访问 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com