D
D
Dmitry Filandor2016-12-30 15:30:56
Programming
Dmitry Filandor, 2016-12-30 15:30:56

How to parse json from a file?

Hello! Guys help me figure out the problem, I've been fighting for several hours, here's what I have:

internal static class Serializer
    {     

        internal static T Deserialize<T>(string fileName)
        {
            string json = System.IO.File.ReadAllText(fileName);
            T res = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
            return res;
        }
    }


    public class Features
    {
        public int satellite { get; set; }
        public int webcams { get; set; }
        public int forecast { get; set; }
    }

    public class Response
    {
        public string version { get; set; }
        public string termsofService { get; set; }
        public Features features { get; set; }
    }

    public class Satellite
    {
        public string image_url { get; set; }
        public string image_url_ir4 { get; set; }
        public string image_url_vis { get; set; }
    }

    public class Forecastday
    {
        public int period { get; set; }
        public string icon { get; set; }
        public string icon_url { get; set; }
        public string title { get; set; }
        public string fcttext { get; set; }
        public string fcttext_metric { get; set; }
        public string pop { get; set; }
    }

    public class TxtForecast
    {
        public string date { get; set; }
        public List<Forecastday> forecastday { get; set; }
    }


//вызов
  string pathToFIle = @"d:\t.txt";
            TxtForecast response = Serializer.Deserialize<TxtForecast>(pathToFIle);
            String[] titles = response.forecastday.Select(item => item.title).ToArray(); // Массив названий
            String[] urls = response.forecastday.Select(item => item.fcttext_metric).ToArray(); // Массив адресов

my file with jason https://yadi.sk/i/dAZeCk8e35wViv
I need to get weather values ​​for 5 days - joxi.ru/QY2LqkJiPlJGA6
no matter how I try, here T res = Newtonsoft.Json.JsonConvert.DeserializeObject(json); always null, although there is string json = System.IO.File.ReadAllText(fileName);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Olohtonov, 2016-12-30
@sgjurano

Trolling:
with open('filename.json') as f:
data = json.load(f)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question