如何在Window Phone 7/8中读取本地.json文件并显示数据?

时间:2013-03-20 03:52:05

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

我尝试了几种不同的方法,但没有运气。注意:在第二个if语句中,我输入了长json字符串。但是,我不想写长json字符串,而是想从json文件中读取。

    private void Pivot_LoadingPivotItem_1(object sender, PivotItemEventArgs e)
    {
        try
        {
            if(e.Item == item1)
            {
                list1.Items.Clear();
                //string stopslist1 = @"[{""Name"":""Communication Sciences""},{""Name"":""Hope Lodge""},{""Name"":""Juniper-Poplar""}]";
                IsolatedStorageFile mysfile = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream fs = mysfile.OpenFile(@"c:\users\prime\documents\visual studio 2012\Projects\BullRunnertest3\BullRunnertest3\stopsA.json",FileMode.Open, FileAccess.Read);
                using (StreamReader re = new StreamReader(fs))
                {
                    string stopslist1 = re.ReadToEnd();
                }
                List<RouteStops> stops = JsonConvert.DeserializeObject<List<RouteStops>>(stopslist1);
                foreach(RouteStops em in stops)
                {
                    string name = em.Name;
                    list1.Items.Add(name);
                }
            }
            else if (e.Item == item2)
            {
                list2.Items.Clear();
                string stopslist2 = @"[{""Name"":""Computer Sciences""},{""Name"":""Hope for Lodge""},{""Name"":""Juniper and Poplar""}]";
                List<RouteStops> stops = JsonConvert.DeserializeObject<List<RouteStops>>(stopslist2);
                foreach (RouteStops em in stops)
                {
                    string name = em.Name;
                    list2.Items.Add(name);
                }
            }
            else......

1 个答案:

答案 0 :(得分:3)

看起来您正试图访问Windows手机上的C://驱动器...这不存在。观看此视频:http://channel9.msdn.com/Series/Building-Apps-for-Windows-Phone-8-Jump-Start/Building-Apps-for-Windows-Phone-8-Jump-Start-04-Files-and-Storage-on-Windows-Phone-8

了解如何从本地存储中访问文件。