using VS 2013, C#
I am trying to use JSON file in my project. Using next code (as in sample project from MS):
Uri dataUri = new Uri("ms-appx:///DataModel/MyData.json");
StorageFile fileToRequest = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string jsonText = await FileIO.ReadTextAsync(fileToRequest);
JsonObject jsonO = JsonObject.Parse(jsonText);
JsonArray jsonA = jsonO["Events"].GetArray();
As result got this:
and next step - exception file not found, but it exists
Also, if the same code launched with sample project from VS - all its ok. Code from sample project:
Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string jsonText = await FileIO.ReadTextAsync(file);
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["Groups"].GetArray();
Debugging of this code got:
So question - why I got exception FileNotFound
, why this file invisible for my project.
Oh finnally found reason
If you want to use JSON file need to make next:
In properties set Build Action: Content
, and Copy to Output Directory: Copy Always
Also found answer here. Also found this and this link useful. Maybe it can be helpful for someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With