C#: string olarak alamadığım arrayi alternatif alma yöntemleri

222744

Gigapat
İlk 5 Haneli Mesajınız!
Katılım
27 Temmuz 2018
Mesajlar
11.217
Makaleler
5
Çözümler
57
Garip bir sorum var.
openweathermap apisini kullanarak dandik bir meteoroloji uygulaması yapmaya çalışıyorum, APIye gönderdiğim GET isteği bana JSON olarak dönüyor. Elimdeki JSON dosyasını da direkt var olarak stream alıp elimdeki değişkenlere atıyorum.
Sorum ise oradaki küçük bir arrayi almakta.

Kod:
//GET İsteği
using (HttpClient client = new HttpClient())
{
    apiString = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&include=current,hourly,daily&apikey={apikey}&units=metric";
    Console.WriteLine(apiString);
    var result = await client.GetStreamAsync(apiString);
    MainData weathermapDeserializer = JsonSerializer.Deserialize<MainData>(result);
    //test amaçlı anlık sıcaklık çıktısı
    Console.WriteLine(weathermapDeserializer.current.temp);
   
}
Kod:
public class MainData
{
    public CurrentData current { get; set; }
}

public class CurrentData
{
    public double temp { get; set; }
    public int Humidity { get; set; }
    //malum array
    public object[] weather { get; set; }  
}

Şu anda her şey mükemmel çalışıyor, tek sorum o arrayi object olarak almak yerine başka bir değerde mi almam daha doğru olur yoksa böyle iyi mi?
 
Garip bir sorum var.
openweathermap apisini kullanarak dandik bir meteoroloji uygulaması yapmaya çalışıyorum, APIye gönderdiğim GET isteği bana JSON olarak dönüyor. Elimdeki JSON dosyasını da direkt var olarak stream alıp elimdeki değişkenlere atıyorum.
Sorum ise oradaki küçük bir arrayi almakta.

Kod:
//GET İsteği
using (HttpClient client = new HttpClient())
{
    apiString = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&include=current,hourly,daily&apikey={apikey}&units=metric";
    Console.WriteLine(apiString);
    var result = await client.GetStreamAsync(apiString);
    MainData weathermapDeserializer = JsonSerializer.Deserialize<MainData>(result);
    //test amaçlı anlık sıcaklık çıktısı
    Console.WriteLine(weathermapDeserializer.current.temp);
  
}
Kod:
public class MainData
{
    public CurrentData current { get; set; }
}

public class CurrentData
{
    public double temp { get; set; }
    public int Humidity { get; set; }
    //malum array
    public object[] weather { get; set; } 
}

Şu anda her şey mükemmel çalışıyor, tek sorum o arrayi object olarak almak yerine başka bir değerde mi almam daha doğru olur yoksa böyle iyi mi?
JSON'da neye karşılık geliyor weather?
 
JSON'da neye karşılık geliyor weather?
O anlık hava durumunun bulutlu, güneşli cinsinden verisine. Anladığım kadarıyla id verisi int olarak verildiği için String olarak alınmıyor da başka ne yapılabilir acaba?
JSON:
{
    "lat": 41.069,
    "lon": 29.0049,
    "timezone": "Europe/Istanbul",
    "timezone_offset": 10800,
    "current": {
        "dt": 1694344541,
        "sunrise": 1694317146,
        "sunset": 1694363014,
        "temp": 26.63,
        "feels_like": 26.63,
        "pressure": 1015,
        "humidity": 44,
        "dew_point": 13.38,
        "uvi": 5.5,
        "clouds": 20,
        "visibility": 10000,
        "wind_speed": 11.32,
        "wind_deg": 20,
        "weather": [
            {
                "id": 801,
                "main": "Clouds",
                "description": "few clouds",
                "icon": "02d"
            }
        ]
    },
 
weather attribute'u için de ayrı bir obje yaratacaksın.
C#:
internal class Program
{
    internal class WeatherObject
    {
        public int id { get; set; }
        public string? main { get; set; }
        public string? description { get; set; }
        public string? icon { get; set; }
    }
    internal class CurrentObject
    {
        public int dt{ get; set; }
        public int sunrise{ get; set; }
        public int sunset{ get; set; }
        public double temp{ get; set; }
        public double feels_like{ get; set; }
        public int pressure{ get; set; }
        public int humidity{ get; set; }
        public double dew_point{ get; set; }
        public double uvi{ get; set; }
        public int clouds{ get; set; }
        public int visibility{ get; set; }
        public double wind_speed{ get; set; }
        public double wind_deg{ get; set; }
        public WeatherObject[]? weather{ get; set; }
    }

    internal class MainObject
    {
        public double lat{ get; set; }
        public double lon{ get; set; }
        public string? timezone{ get; set; }
        public int timezone_offset{ get; set; }
        public CurrentObject? current { get; set; }
    }
    private static string s_jsonText = @"{
""lat"": 41.069,
""lon"": 29.0049,
""timezone"": ""Europe/Istanbul"",
""timezone_offset"": 10800,
""current"":
{
    ""dt"": 1694344541,
    ""sunrise"": 1694317146,
    ""sunset"": 1694363014,
    ""temp"": 26.63,
    ""feels_like"": 26.63,
    ""pressure"": 1015,
    ""humidity"": 44,
    ""dew_point"": 13.38,
    ""uvi"": 5.5,
    ""clouds"": 20,
    ""visibility"": 10000,
    ""wind_speed"": 11.32,
    ""wind_deg"": 20,
    ""weather"": [
        {
            ""id"": 801,
            ""main"": ""Clouds"",
            ""description"": ""few clouds"",
            ""icon"": ""02d""
        }
    ]
}
}";
    static void Main(string[] args)
    {
        MainObject? mainObject = JsonSerializer.Deserialize<MainObject>(s_jsonText);
        Console.ReadLine();
    }
}
 
Bunun yerine WebClient kullanıp gelen verileri NewtonSoft Json Nuget paketi ile parse edip aşağıdaki gibi kullanmak daha kısa ve kolay olmaz mıydı? API Key'im olmadığından kodu test edemiyorum fakat mantıken çalışacaktır.

C#:
            using (WebClient client = new WebClient())
            {
                string apiString = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&include=current,hourly,daily&apikey={apikey}&units=metric";
                JObject json = JObject.Parse(client.DownloadString(apiString));
                Console.WriteLine(json["current"]["temp"]);
            }
 

Technopat Haberler

Yeni konular

Yeni mesajlar

Geri
Yukarı