How can I parse a JSON file from Assets folder in UWP - Closed

1.4k views Asked by At

I am new developing windows UWP app and I am trying to parse a JSON file from my assets folder. I've seen many tutorials, but when I tried they don't work. Please, someone help me to parse it and if you can with an example.

I am using VS2017 and C#;

This is the library that I want to use:

using Windows.Data.Json;

My code is:

private Uri appUri = new Uri("ms-appx:///Assets/marker.json");
private string title;
private void ConverJSONtoObjects()
        {
            try
            {
                Uri appUri = new Uri(fileName);//File name should be prefixed with 'ms-appx:///Assets/* 
                StorageFile anjFile = StorageFile.GetFileFromApplicationUriAsync(appUri).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); 
                string jsonText = FileIO.ReadTextAsync(anjFile).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); 
                JsonArray obj = JsonValue.Parse(jsonText).GetArray();
                for (uint i = 0; i < obj.Count; i++)
                {
                    title = obj.GetObjectAt(i).GetNamedString("name");
                }
                message("Place", title);
            }
            catch (Exception ex)
            {
                message("Error", ex.ToString());
            }
            
        }

I'm getting this error: error handled by Exception

My file is like this:

[
{
"id":1,
"name":"Cabañas Nuevo Amanecer",
"lat":"18.402785",
"lng":"-70.094953",
"type":"Normal",
"phone":"No Disponible",
"price":"DOP 500",
"image":"http://i65.tinypic.com/10mif69.jpg"
},
{
"id":2,
"name":"Cabañas Costa Azul",
"lat":"18.424746",
"lng":" -69.990333",
"type":"Lujosa",
"phone":"(809) 539-6969",
"price":"DOP 4453",
"image":"http://i64.tinypic.com/wcd5b8.png"
}
]

Solution (updated 2021/11/7)

Hi, for those how have the same problem, the issue was in the file properties: The build action needs to be Content for being able to call a file from the Assets folder in UWP

1

There are 1 answers

2
Cyprien Autexier On

You are probably looking for one of the innumerable JSON libraries. You should start with Json.Net, which is the most popular choice. (You can have a look at alternatives such as Service Stack Text, FastJsonParser or Jil).

A simple way would be to declare a class matching your expected data schema:

public class PointOfInterest
{
    public int Id { get; set; }
    public string Name { get; set; }
    // ...
}

And the use deserialization.:

var poiArray = JsonConvert.DeserializeObject<PointOfInterest[]>(jsonString, new JsonSerializerSettings 
    { 
        ContractResolver = new CamelCasePropertyNamesContractResolver() 
    });

Edit so with your updated requirement you would have to do things like this :

var array = JArray.Parse(jsonString);

foreach(JObject item in array){
   var poi = new PointOfInterest()
   poi.Id = (int)item.GetNamedNumber("id");
   //...
}

The official documentation is pretty straightforward.