błąd inicjalizacji z json

0

witam
Mam pewien problem otóż, z pewnego api pobrałem ciąg znaków zapisany jako json, który przechowuje w zmiennej typu string. Chciałem przypisać wartości zmiennych z tego jsona do obiektu i mam pewien problem. Mianowicie występuje u mnie błąd przy inicjalizacji wartości do pól obiektu: ArgumentException

An unhandled exception of type 'System.ArgumentException' occurred in Newtonsoft.Json.dll

Additional information: Accessed JArray values with invalid key value: "description". Int32 array index expected. 

i nie wiem gdzie zrobiłem błąd:(. Czy moglibyście mi pomóc ? A może znacie jakiś szybszy sposób na uzupełnienie tych pól obiektu niż wpisywanie ręczne ?

Klasy dla json

 public class Rootobject
        {
        public Coord coord { get; set; }
        public Weather weather { get; set; }
        public string _base { get; set; }
        public Main main { get; set; }
        public Wind wind { get; set; }
        public Snow snow { get; set; }
        public Clouds clouds { get; set; }
        public int dt { get; set; }
        public Sys sys { get; set; }
        public int id { get; set; }
        public string name { get; set; }
        public int cod { get; set; }
        }

        public class Coord
        {
        public float lon { get; set; }
        public float lat { get; set; }
        }

        public class Main
        {
        public float temp { get; set; }
        public int pressure { get; set; }
        public int humidity { get; set; }
        public float temp_min { get; set; }
        public float temp_max { get; set; }
        }

        public class Wind
        {
        public float speed { get; set; }
        public float deg { get; set; }
        }

        public class Snow
        {
        }

        public class Clouds
        {
        public int all { get; set; }
        }

        public class Sys
        {
        public int type { get; set; }
        public int id { get; set; }
        public float message { get; set; }
        public string country { get; set; }
        public int sunrise { get; set; }
        public int sunset { get; set; }
        }

        public class Weather
        {
        public int id { get; set; }
        public string main { get; set; }
        public string description { get; set; }
        public string icon { get; set; }
        }

Plik json:
{"coord":{"lon":21.01,"lat":52.23},"weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13n"}],"base":"stations","main":{"temp":268.53,"pressure":1040,"humidity":58,"temp_min":265.93,"temp_max":270.37},"visibility":10000,"wind":{"speed":3.1,"deg":70},"snow":{"1h":0.36},"clouds":{"all":0},"dt":1451416859,"sys":{"type":1,"id":5374,"message":0.0334,"country":"PL","sunrise":1451371521,"sunset":1451399471},"id":756135,"name":"Warsaw","cod":200}

Główna klasa:

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("wprowadz nazwę miescowości: ");
            string town = Console.ReadLine();
            string fullDocJson = Http.GetJson(town);

            JObject parseJObject = JObject.Parse(fullDocJson);

            Rootobject ro = new Rootobject
            {
                _base = "",
                clouds = new Clouds { all = (int)parseJObject["clouds"]["all"] },
                cod = (int)parseJObject["cod"],
                coord = new Coord { lat = (float)parseJObject["coord"]["lat"],lon = (float)parseJObject["coord"]["lon"]},
                dt = (int)parseJObject["dt"],
                id = (int)parseJObject["id"],
                main = new Main
                {
                    humidity = (int)parseJObject["main"]["humidity"],pressure = (int)parseJObject["main"]["pressure"],
                    temp = (float)parseJObject["main"]["temp"],temp_max = (float)parseJObject["main"]["temp_max"],temp_min = (float)parseJObject["main"]["temp_min"]
                },
                name = (string)parseJObject["name"],
                snow = new Snow(),
                sys = new Sys
                {
                    country = (string)parseJObject["sys"]["country"],id = (int)parseJObject["sys"]["id"],message = (float)parseJObject["sys"]["message"],
                    sunrise = (int)parseJObject["sys"]["sunrise"],sunset = (int)parseJObject["sys"]["sunset"],type = (int)parseJObject["sys"]["type"]
                },
                weather = new Weather 
                { 
                    description = (string)parseJObject["weather"]["description"],icon = (string)parseJObject["weather"]["icon"], 
                    id = (int)parseJObject["weather"]["id"],main = (string)parseJObject["weather"]["main"]
                },
                wind = new Wind { deg = (float)parseJObject["wind"]["deg"],speed = (float)parseJObject["wind"]["speed"]}
            };
            
            Console.WriteLine(ro.name);
            Console.ReadLine();
        }
    }
}

Nie rozumiem dlaczego w jsonie jest takie coś: "weather":[{"id":600,"main":"Snow","description":"light snow","icon":"13n"}] jakby to miała być tablica, ale chyba by było to trochę bez sensu..

2

No, po to się chyba używa Json.NET, żeby nie robić nic ręcznie.

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(json);
0

@somekind jesteś wielki :) dzięki bardzo.
A tak swoją drogą to może wiesz co było źle w moim kodzie ?

0

a już żeby nie zaczynać nowego wątku chciałem się zapytać czy istnieje możliwość sprawdzenia istnienia... nie wiem za bardzo jak to nazwać propercji w propercji. Chodzi mi o coś takiego:

np. plik json
{"coord":{"lon":21.01,"lat":52.23}

i chciałbym sprawdzić czy istnieje lat
Jeśli chodzi o kod to chciałbym zrobić coś takiego:

parseJObject = JObject.Parse(fullDocJson);
                var propertyMsg = parseJObject.Property("coord").Property("lat") // nie wiem właśnie jak to zapisać ; 

niby bym mógł to sprawdzić pętlą foreach ale czy da rade to zrobić w jednym zapisie ?

ok już sobie poradziłęm wystarczyło zamiast drugiego Property("lat") zamienić na Values("lat")

1 użytkowników online, w tym zalogowanych: 0, gości: 1