使用自定义数据源从JSON获取数据

时间:2015-05-07 13:50:48

标签: c# json windows-phone-8 windows-phone json.net

首先,我是Windows Phone开发的新手。目前我尝试在本地保存JSON文件,然后读取它并使用这些对象。

我可以在本地保存JSON但现在我想使用我的数据源读取它。

这是我的数据源:

public sealed class OrganizationDataSource
    {
        private static OrganizationDataSource _organizationDatasource = new OrganizationDataSource();

        private static string jsonPath = "organizationdata.json";

        private static List<OrganizationObject> _organizationList = new List<OrganizationObject>();

        public static List<OrganizationObject> organizationList
        {
            get { return _organizationList; }
        }

        public static async Task AddOrganization(string jsonObjects)
        {
            await AddOrganizationToList(jsonObjects, true);
        }

        /// <summary>
        /// Alle events worden lokaal opgehaald bij het starten van de applicatie.
        /// </summary>
        private async Task GetOrganizationFromJson()
        {
            if (organizationList.Count != 0)
                return;

            var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            var file = await folder.GetFileAsync(jsonPath);
            var jsonText = await Windows.Storage.FileIO.ReadTextAsync(file);
            Debug.WriteLine(jsonText);
            await AddOrganizationToList(jsonText, false);
        }

        /// <summary>
        /// Statische methode waarmee alle events lokaal opgehaald worden uit een Json bestand.
        /// </summary>
        public static async Task getLocalOrganization()
        {
            await _organizationDatasource.GetOrganizationFromJson();
        }

        private static async Task AddOrganizationToList(string organization, bool newOrganization)
        {
            if (!newOrganization && _organizationList.Count != 0)
                return;
            try
            {
                Organization orgItem = JsonConvert.DeserializeObject<Organization>(organization);
                Debug.WriteLine(orgItem);
                _organizationList = orgItem.organizations;
                string content = organization;
                Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile sampleFile = await localFolder.CreateFileAsync(jsonPath,CreationCollisionOption.ReplaceExisting);
                await FileIO.WriteTextAsync(sampleFile, content);
            }
            catch (JsonReaderException jrex)
            {
                Debug.WriteLine(String.Format("AddNewsToList JsonReasException: {0}", jrex.Message));
            }
            catch (ArgumentException argex)
            {
                Debug.WriteLine(String.Format("AddNewsToList ArgumentException: {0}", argex.Message));
            }
            catch (NullReferenceException nrex)
            {
                Debug.WriteLine(String.Format("AddNewsToList NullReferenceException: {0}", nrex.Message));
                return;
            }
        }
    }

现在在我的XAML文件后面的代码中,我试图获取organisationList。 我按照以下方式执行此操作:

 private void BindData()
        {
            try
            {
                orgItems = OrganizationDataSource.organizationList;
                Debug.WriteLine(orgItems.Count());
                foreach (OrganizationObject obj in orgItems)
                {
                    Debug.WriteLine(obj.organization_alias);
                }
            }  
            catch (KeyNotFoundException)
            {
                NavigationService.GoBack();
            }
        }

现在的问题是我的计数总是返回0。 有人有想法吗?

0 个答案:

没有答案