How do you open a file in and read JSON in Xamarin Forms? This is how I save the file:
//Serialize
var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
};
string json = Newtonsoft.Json.JsonConvert.SerializeObject(syncItems, Newtonsoft.Json.Formatting.None, jsonSerializerSettings);
//Save to disk
using (var file = File.Open(GTOSerializePath, FileMode.OpenOrCreate, FileAccess.Write))
using (var strm = new StreamWriter(file))
{
strm.Write(json);
}
And this is how I attempt to read it:
using (var strr = new StreamReader(GTOSerializePath))
{
json = strr.ReadToEnd().Trim();
}
//Deserialize object graph
var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
};
items = Newtonsoft.Json.JsonConvert.DeserializeObject<SyncItems>(json, jsonSerializerSettings);
But I get error: Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content.