title: Get JSON from NSURL in c# xamarin using NSURLSession
body: <p>I'm having trouble with pulling json using nsurlsession in xamarin c#. I can't figure out how to work with the NSURLSessionDownloadDelegate, DidFinishDownloading method converting the NSURL location to JSON. I tried 3 different solutions below, I want to access the JSON and make it NSDictionary for me to work with. </p>
  
  <pre><code> public static void getApps () 
  {
  var config = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration("com.SimpleBackgroundTransfer.BackgroundSession");
  var session = NSUrlSession.FromConfiguration(config, (NSUrlSessionDelegate) new NetworkDelegate(), new NSOperationQueue());
  var url = NSUrl.FromString("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=25/json");
  var request = NSUrlRequest.FromUrl(url);
  var downloadTask = session.CreateDownloadTask (request);
  downloadTask.Resume ();
  }
  
  public class NetworkDelegate : NSUrlSessionDownloadDelegate {
      public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
      {   
      //solution1
      FileStream file = File.OpenRead (location.Path);
      var jsonData = NSJsonSerialization.Deserialize(data, NSJsonReadingOptions.MutableContainers, error);
      //error: The name 'error' does not exist in the current context"
  
      //solution2
      NSData data = NSFileManager.DefaultManager.Contents(location.Path);
      App app1 = JsonConvert.DeserializeObject&lt;App&gt;(File.ReadAllText(location.Path));
      //output: top25.App
  
      //solution3
      var json = JsonConvert.DeserializeObject(location.Path); 
      //error: "NewtonSoft.Json.JsonReaderException:
  Error parsing comment. Expected: *, got U. Path '', line 1, position 1."
      }
  }
  </code></pre>
  
  <p>Update: Worked with the code below but now I am getting the following error ( Breaks on the last line (NSDictionary) when deserializing. 
  ):</p>
  
  <pre><code>NSData data = NSFileManager.DefaultManager.Contents(location.Path); 
  NSString dataString = NSString.FromData(data, NSStringEncoding.UTF8);
  NSDictionary jsonDict = JsonConvert.DeserializeObject&lt;NSDictionary&gt;(File.ReadAllText(dataString)); 
  </code></pre>
  
  <blockquote>
    <p>"System.IO.DirectoryNotFoundException: Could not find a part of the
    path (path + JSON that I need)".</p>
  </blockquote>
  
  <p>Update: This prints the json as a .net object in the console. How to convert to nsdictionary?</p>
  
  <pre><code>NSData data = NSFileManager.DefaultManager.Contents(location.Path);
  NSString dataString = NSString.FromData(data, NSStringEncoding.UTF8);
  var json = JsonConvert.DeserializeObject(dataString); 
  Console.WriteLine ("json {0}", json);
  </code></pre>
  
  <p><strong>RESOLVED:</strong> </p>
  
  <pre><code>NSData data= NSFileManager.DefaultManager.Contents(location.Path);
  NSError err;
  NSDictionary jsonData = (NSDictionary)NSJsonSerialization.Deserialize(data, NSJsonReadingOptions.MutableContainers, out err);
  </code></pre>