locked
Grid App template deserializing Json

    Question

  • Hi all, i'm trying to create a new metro app for w8.1 based on grid app template, but i have a question. Why in the template they deserialize in this way:

    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();
    
                foreach (JsonValue groupValue in jsonArray)
                {
                    JsonObject groupObject = groupValue.GetObject();
                    SampleDataGroup group = new SampleDataGroup(groupObject["UniqueId"].GetString(),
                                                                groupObject["Title"].GetString(),
                                                                groupObject["Subtitle"].GetString(),
                                                                groupObject["ImagePath"].GetString(),
                                                                groupObject["Description"].GetString());
    
                    foreach (JsonValue itemValue in groupObject["Items"].GetArray())
                    {
                        JsonObject itemObject = itemValue.GetObject();
                        group.Items.Add(new SampleDataItem(itemObject["UniqueId"].GetString(),
                                                           itemObject["Title"].GetString(),
                                                           itemObject["Subtitle"].GetString(),
                                                           itemObject["ImagePath"].GetString(),
                                                           itemObject["Description"].GetString(),
                                                           itemObject["Content"].GetString()));
                    }
                    this.Groups.Add(group);
                }

    Insead of doing something more simple, like this:

    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
                string jsonText = await FileIO.ReadTextAsync(file);
    
                Groups = JsonConvert.DeserializeObject<ObservableCollection<SampleDataGroup>>(jsonText);

    What's the difference ? I can't really understand it.
    Thank you in advance.

    Regards,

    Marco

    Friday, August 22, 2014 12:27 PM

Answers