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

3

我尝试了几种不同的方法,但还没有成功。注意:在第二个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......

如果您使用的是 F#,它已经有 JSON 数据提供程序可以读取。如果您能将 JSON 文件转储到其中,那就太好了。 - kunjee
不,我只使用C#,你有什么建议吗?谢谢。 - Vasim
我认为你已经得到了答案。:) 如果还有任何问题,请让我知道。 - kunjee
1个回答

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接