Parse Json C # a object

0

What would be the best way to parse this Json?

  

link

I tried like this, but it does not give me data

var resultJson2 = Json.deserialize<List<ClimaHour>>(result.Result);
public class ClimaHour {
    public  List<int> couldCover { get; set; }
    public  List<string> dayOfWeek { get; set; }
    public  List<string>dayOrNight { get; set; }
    public  List<int> expirationTimeUtc { get; set; }
    public  List<int> iconCode { get; set; }
    public  List<int> iconCodeExtend { get; set; }
    public  List<int> precipChance { get; set; }
    public  List<string> precipType { get; set; }
    public List<decimal> pressureAltimeter { get; set; }
}
    
asked by Ariel Octavio D'Alfeo 03.07.2018 в 17:27
source

1 answer

0

In theory you should only read the content of the json, save it in string and pass that variable for its deserialization .

string jsonPath = System.IO.File.ReadAllText(json);
var jsonD = JsonConvert.DeserializeObject<ClimaHour>(jsonPath);

The only strange thing I noticed in your question was that you were trying to deserialize a list ( List<ClimaHour> ) of ClimaHour when it should only be Objeto .

Another thing is that you use Json.deserialize ... I searched for that library and I did not find it = /, so the answer I gave is using Newtonsoft .

Now, as you indicate a url for said JSON and this one is in Google Drive , we must build a function that returns a valid url for the deserialization of the json .

Helpers helper = new Helpers();
using (var web = new WebClient())
{
   string data = string.Empty;
   string url = helper.GetGoogleDriveDownloadLinkFromUrl("https://drive.google.com/file/d/1A9-6o99kcIqRM3CEaXxYwh_Dv-vUkvnM/view");
   data = web.DownloadString(url);
   var json = JsonConvert.DeserializeObject<ClimaHour>(data);
}

And the implementation of the method GetGoogleDriveDownloadLinkFromUrl , which is inside the class Helpers

public class Helpers
{
   public string GetGoogleDriveDownloadLinkFromUrl(string url)
   {
      int index = url.IndexOf("id=");
      int closingIndex;

      if (index > 0)
      {
         index += 3;
         closingIndex = url.IndexOf('&', index);

         if (closingIndex < 0)
            closingIndex = url.Length;
      }
      else
      {
         index = url.IndexOf("file/d/");

         if (index < 0)
            return string.Empty;

            index += 7;
            closingIndex = url.IndexOf('/', index);

            if (closingIndex < 0)
            {
               closingIndex = url.IndexOf('?', index);

               if (closingIndex < 0)
                  closingIndex = url.Length;
            }
       }
       return $"https://drive.google.com/uc?id={url.Substring(index, closingIndex - index)}";
     }
  }

I can not take all the credit and I mention that the function that the url creates extracted it from this link ; the way to call this function can be creating a class (as I did) or within the same class that reads the JSON , that I leave at your discretion colleague

    
answered by 03.07.2018 / 18:03
source