【问题标题】:C# store app can't load json dataC# 商店应用程序无法加载 json 数据
【发布时间】:2014-03-14 17:20:46
【问题描述】:

我是 C# Store 应用程序的初学者,我有一个 JSON 文件,我想在我的屏幕磁贴中看到它。问题是我的方法根本不会检索任何数据。 第二个问题,为了在我的屏幕上显示数据,它必须是一个可观察的集合吗?

public class DataConnection
{
    public DataConnection()
    {

    }

    public async Task getDataFromJson()
    {
        Uri dataUri = new Uri("ms-appx:///DataModel/ProductData.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)
        {
            List<ProductGroup> groupList = new List<ProductGroup>();
            JsonObject groupObject = groupValue.GetObject();
            ProductGroup group = new ProductGroup(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 ProductItem(itemObject["UniqueId"].GetString(),
                                                   itemObject["Title"].GetString(),
                                                   itemObject["Artikelnummer"].GetString(),
                                                   itemObject["VerkoopprijsInBtw"].GetString(),
                                                   itemObject["VerkoopprijsExBtw"].GetString(),
                                                   itemObject["Inkoopprijs"].GetString(),
                                                   itemObject["ActualStock"].GetString(),
                                                   itemObject["Marge"].GetString(),
                                                   itemObject["Eenheid"].GetString(),
                                                   itemObject["ImagePath"].GetString(),
                                                   itemObject["Description"].GetString()));

            }
            groupList.Add(group);
        }
    }
}

问题似乎在于: Uri dataUri = new Uri("ms-appx:///DataModel/ProductData.json");

        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);

调试器只是结束方法并继续进行而不检索任何数据。 json 文件中的数据经过测试并正确无误,所以这不是问题。 我从标准 Gridapp 复制了这个方法,所以代码应该没问题,我知道问题(可能)发生在 StorageFile 中。我错过了什么吗?

任何提示都会很好!

提前致谢。

【问题讨论】:

  • 首先,我会检查您的路径并确保完整路径正确(您可能已经这样做了,但仍然需要注意)。其次,我会查看file 是否填充了正确的文件。在它之后添加一个断点并查看它是否正在加载。您也可以致电GetBasicPropertiesAsync 并查看其Size。第三,您的集合不需要ObservableCollection,除非您在将其加载到 UI 后对其进行填充或修改。如果它是静态列表,则不是(但请确保在加载数据之后设置源 )。

标签: c# json windows-store-apps


【解决方案1】:

问题是由您的异步调用引起的。

尝试如下调用GetFileFromApplicationUriAsync 同步

StorageFile file = StorageFile.GetFileFromApplicationUriAsync(dataUri).Result;

请注意,我省略了 await 关键字并获取了 Result 属性。

【讨论】:

  • 我遇到了同样的问题,试试你的方法。它有效!
  • 然而,这个过程并不总是成功的,因为系统会抛出一个System.UriFormatException。如果我使用StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);,它会抛出System.Collections.Generic.KeyNotFoundException
【解决方案2】:

感谢您的回答,但问题很愚蠢。对于有同样问题的人:

我设法修复它以将 json 文件也放入您项目的调试器中的 \bin\Debug\AppX 下(它没有自动进入那里,您根本看不到任何东西或不会检索数据,如果您想像我使用存储文件一样获取数据)。

感谢您的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    相关资源
    最近更新 更多