Advertisement
James-Ko

ServiceEnvoyExtractor class

Mar 28th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1.     /// <summary>
  2.     /// Static class responsible for deserializing JSON to ServiceEnvoys.
  3.     /// </summary>
  4.     public static class ServiceEnvoyExtractor
  5.     {
  6.         private const string ApplicationUriPrefix = "ms-appx:///",
  7.             AssetUriPrefix = ApplicationUriPrefix + "Assets/",
  8.             DataUriPrefix = ApplicationUriPrefix + "Data/";
  9.  
  10.         private static Uri ServiceRecordsPath = new Uri(DataUriPrefix + "ServiceRecords.json");
  11.  
  12.         public async static Task<ObservableCollection<ServiceEnvoy>> ExtractServiceEnvoys()
  13.         {
  14.             //get string from file
  15.             var file = await StorageFile
  16.                 .GetFileFromApplicationUriAsync(ServiceRecordsPath);
  17.             string json = await FileIO.ReadTextAsync(file);
  18.  
  19.             var collection = new ObservableCollection<ServiceEnvoy>();
  20.  
  21.             //parse json into collection
  22.             foreach (var value in JsonObject.Parse(json)["Services"].GetArray())
  23.             {
  24.                 var obj = value.GetObject();
  25.                 collection.Add(new ServiceEnvoy(
  26.                     obj["Name"].GetString(),
  27.                     new BitmapImage(new Uri(
  28.                         AssetUriPrefix + obj["Image"].GetString()))));
  29.             }
  30.  
  31.             return collection;
  32.         }
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement