Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Open File

$
0
0

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.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>