Answer the question
In order to leave comments, you need to log in
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(); // Массив адресов
Answer the question
In order to leave comments, you need to log in
Trolling:
with open('filename.json') as f:
data = json.load(f)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question