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"}
コントローラーで、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
最後に、インスタンス変数をビューにレンダリングします。
<script>
x = "<%= @it %>"
</script>
コンソールにxenterと入力すると、jsonの形式が少し異なり、問題が発生する可能性があります。
"{"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":"2010-10-01","value":"2.39307"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2011-01-01","value":"0.07892"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2011-04-01","value":"2.47721"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2011-07-01","value":"1.27864"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2011-10-01","value":"4.09234"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2012-01-01","value":"1.96053"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2012-04-01","value":"1.25266"},{"realtime_start":"2012-12-27","realtime_end":"2012-12-27","date":"2012-07-01","value":"3.10599"}]}}"
私の質問は、キーと値のペアの1つにアクセスするにはどうすればよいですか?javascriptで解析する必要がありますか?ドット表記でこれを行う方法はありますか?