检索JSON数据并保留特定值的记录

时间:2019-05-15 10:34:12

标签: javascript json

我正在尝试构建一个简单的代码,在其中获取一些JSON数据(天气信息)并将温度记录到控制台。这是我的示例代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            {
                button1.BackColor = Color.Green;
                button2.BackColor = Color.Lavender;
                button3.BackColor = Color.Lavender;
                button4.BackColor = Color.Lavender;
                button5.BackColor = Color.Lavender;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Lavender;
            button2.BackColor = Color.Green;
            button3.BackColor = Color.Lavender;
            button4.BackColor = Color.Lavender;
            button5.BackColor = Color.Lavender;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Lavender;
            button2.BackColor = Color.Lavender;
            button3.BackColor = Color.Green;
            button4.BackColor = Color.Lavender;
            button5.BackColor = Color.Lavender;
        }
    }
}

我得到提示输入城市名称,并且该值已正确保存到变量中。但是,我的控制台没有记录温度。

以下是JSON响应的示例:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Simple Weather Application</title>
  </head>

<body>    
<script>
var cityName = prompt("Please enter city name", "London");

var request = new XMLHttpRequest()

request.open('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&appid=65a3719d36e2d698392212cd888b5ccf', true)
request.onload = function() {
  // Begin accessing JSON data here
  var data = JSON.parse(this.response)

  if (request.status >= 200 && request.status < 400) {
    data => {
      console.log(main.temp)
    }
  } else {
    console.log('error')
  }
}
request.send()    
</script>

  </body>
</html>

知道我可能做错了什么吗?

预先感谢您的帮助。

我已经搜索了一些有关如何执行此操作的代码示例。

我想让控制台记录温度。 另外,有人知道如何将温度从开氏温度转换为摄氏温度吗?

4 个答案:

答案 0 :(得分:0)

基础对象上没有属性temp-它在嵌套的data对象中:

console.log(data.main.temp);

答案 1 :(得分:0)

<script>
var cityName = prompt("Please enter city name", "London");

var request = new XMLHttpRequest()

request.open('GET', `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=65a3719d36e2d698392212cd888b5ccf`, true)
request.onload = function() {
  // Begin accessing JSON data here
  var data = JSON.parse(this.response)

  if (request.status >= 200 && request.status < 400) {
      // kelvin to celsius formula is celsius = Kelvin - 273.15
      console.log(`${data.main.temp - 273.15}C`)

  } else {
    console.log('error')
  }
}
request.send()    
</script>

答案 2 :(得分:0)

<script>
var cityName = prompt("Please enter city name", "London");

var request = new XMLHttpRequest()

request.open('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&appid=65a3719d36e2d698392212cd888b5ccf', true)
request.onload = function() {
  // Begin accessing JSON data here
  var data = JSON.parse(this.response)

  if (request.status >= 200 && request.status < 400) {
      ((main)=>{
          console.log(main.temp)
      })(data.main)
  } else {
    console.log('error')
  }
}
request.send()    
</script>

答案 3 :(得分:0)

library(xts)
library(shiny)
library(shinydashboard)
library(dygraphs)
library(dplyr)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    dygraphOutput('plot1')
  )
)

server <- function(input, output, session) {

  m_df <- data.frame(date=as.Date(zoo::as.yearmon(time(mdeaths))), Y=as.matrix(mdeaths))

  subdata <- reactive({
    if(!is.null(input$plot1_date_window)){
      subdata <- m_df[m_df$date >= as.Date(input$plot1_date_window[1]) & m_df$date <= as.Date(input$plot1_date_window[2]), ]
      subdata$cumsum <- cumsum(subdata$Y)
      subdata$Y <- NULL
    } else {
      subdata <- NULL
    }

    return(subdata)
  })

  subdata_d <- subdata %>% debounce(100)

  output$plot1 <- renderDygraph({
    input_xts <- xts(select(m_df, -date), order.by = m_df$date)
    if(is.null(subdata_d())){
      final_xts <- input_xts
    } else {

      subdata_xts <- xts(select(subdata_d(), - date), order.by = subdata_d()$date)
      final_xts <- cbind(input_xts, subdata_xts)
    }

    p <- dygraph(final_xts) %>% dySeries(name="Y") %>%
      dyRangeSelector(retainDateWindow = TRUE)

    if("cumsum" %in% names(final_xts)){
      p <- dySeries(p, name="cumsum", axis = "y2")
    }

    p
  })

}

shinyApp(ui, server)