【问题标题】:C# Newtonsoft.Json SelectToken not getting values from APIC# Newtonsoft.Json SelectToken 未从 API 获取值
【发布时间】:2020-08-03 06:17:47
【问题描述】:

我正在尝试编写一个控制台应用程序,该应用程序将从不同的天气 API 中挖掘数据,将它们写入屏幕,然后将它们保存到磁盘。我可以显示来自 Dark Sky 的值,但我无法从 Open Weather 获取值。

下面是代码,从一个类开始

using System;
using System.Collections.Generic;
using System.Text;

namespace WeatherAPIs.Models
{
    class API_URL
    {
        //This Class takes all the relevant values such as latitude, longitude, Barrie city code, API keys, to crate 
        public static string DarkSkyBaseURL = "https://api.darksky.net/forecast/{Key}/44.4074,-79.6567?units=si";
        public static string OpenWeatherBaseURL = "https://api.openweathermap.org/data/2.5/forecast?id=5894171&units=metric&APPID={Key}";
        public static string WeatherBitBaseURL = "https://api.weatherbit.io/v2.0/current?&lat=44.4074&lon=-79.6567&key={Key}";
    }
}

这是主要代码:

using System;
using WeatherAPIs.Models;
using System.Net.Http;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WeatherAPIs
{
    class API_Data_Mine
    {
        static void Main(string[] args)
        {
            //Defines The client as WebClient and setup the HTTP Header
            using var client = new WebClient();
            client.Headers.Add("User-Agent", "C# Weather API");
            client.Headers.Add("Accept", "application/json");
            client.Headers.Add("Content-Type", "application/json");

            //Download the JSON result from the DarkSkyBaseURL on API_URL Class
            string darkskyresult = client.DownloadString(API_URL.DarkSkyBaseURL);
            var darkskyjson = JsonConvert.DeserializeObject(darkskyresult);
            //Print int the screen the result of the raw and unbeauty JSON download, wait for the user to press a key to resume
            Console.WriteLine(darkskyresult);
            Console.WriteLine("\npress any key to resume.");
            Console.ReadKey();

            string openWResult = client.DownloadString(API_URL.OpenWeatherBaseURL);
            var openWJson = JsonConvert.DeserializeObject(openWResult);
            //Print int the screen the result of the raw and unbeauty JSON download, wait for the user to press a key to resume
            Console.WriteLine(openWResult);
            Console.WriteLine("\npress any key to resume.");
            Console.ReadKey();

            //Parse and beautify the JSON Output from DarkSky URL
            JObject JDarkSKy = JObject.Parse(darkskyresult);
            JObject JopenW = JObject.Parse(openWResult);

            ////Write the beautified JSON from DarkSKy on screen and wait for the user to press a key to exit
            Console.WriteLine(JDarkSKy);
            Console.WriteLine("\nPress any key to resume.");
            Console.ReadKey();

            Console.WriteLine(JopenW);
            Console.WriteLine("\nPress any key to resume.");
            Console.ReadKey();

            ///Mine the JSON selected JSON data and print it on screen

            var DSTemperature = (string)JDarkSKy.SelectToken("currently.temperature");
            var DSPressure = (string)JDarkSKy.SelectToken("currently.pressure");
            var DSWindSpeed = (string)JDarkSKy.SelectToken("currently.windSpeed");
            var DSRealFeel = (string)JDarkSKy.SelectToken("currently.apparentTemperature");
            var DSUvIndex = (string)JDarkSKy.SelectToken("currently.uvIndex");

            var OWTemperature = (string)JopenW.SelectToken("list.main.temp");
            var OWPressure = (string)JopenW.SelectToken("list.main.pressure");
            var OWWindSpeed = (string)JopenW.SelectToken("list.wind.speed");
            var OWRealFeel = (string)JopenW.SelectToken("list.main.feels_like");

            //UV only works wqith paid keys
            //var OWUvIndex = (string)JDarkSKy.SelectToken("list.main.");

            Console.WriteLine("Temperature on Dark Sky is - " + DSTemperature);
            Console.WriteLine("Pressure on Dark Sky is - " + DSPressure);
            Console.WriteLine("Wind Spped on Dark Sky is - " + DSWindSpeed);
            Console.WriteLine("Real Feel on Dark Sky is - " + DSRealFeel);
            Console.WriteLine("Ultra Violet Index on Dark Sky is - " + DSUvIndex);

            Console.WriteLine("Temperature on Open Weather is - " + OWTemperature);
            Console.WriteLine("Pressure on Open Weather is - " + OWPressure);
            Console.WriteLine("Wind Spped on Open Weather is - " + OWWindSpeed);
            Console.WriteLine("Real Feel on Open Weather is - " + OWRealFeel);

            //Console.WriteLine("\nPress any key to resume.");            
            Console.ReadKey();
        }
    }
}

我需要做什么才能从 Open Weather 获取数据?

【问题讨论】:

  • 有什么问题?看看响应的结构,并像其他人一样做同样的事情
  • 使用app.quicktype.io 几乎可以更轻松地解决这些问题中的每一个

标签: c# json api


【解决方案1】:

openweathermap 返回 list 作为数组而不是对象。所以你应该相应地更新你的选择令牌。

尝试使用[0] 获取第一个对象的数据。

var OWTemperature = (string)JopenW.SelectToken("list[0].main.temp");
var OWPressure = (string)JopenW.SelectToken("list[0].main.pressure");
var OWWindSpeed = (string)JopenW.SelectToken("list[0].wind.speed");
var OWRealFeel = (string)JopenW.SelectToken("list[0].main.feels_like");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2022-01-10
    相关资源
    最近更新 更多