Trks
Trks

Reputation: 27

Xamarin Forms: How to Read Data from Json File from the Phonestorage of my android smartphone

im trying to read Data From A Json File that is in the Storage of the Phone that uses the App. How can I Do that ?

Upvotes: 0

Views: 1031

Answers (1)

haldo
haldo

Reputation: 16701

This is how I would do it. Additional information can be found in File Handling in Xamarin.Forms:

// get file name
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "YourJsonFile.json");
// read file
string json = File.ReadAllText(fileName);

// deserialize/parse json
var data = JsonConvert.DeserializeObject<Data>(json);
// or
var jObject = JObject.Parse(json); 

The above assumes use of Newtonsoft.Json, however, similar can be achieved using System.Text.Json.

Upvotes: 1

Related Questions