如何修复文件 .json 未找到

huangapple go评论94阅读模式
英文:

How to fix file .json not found

问题

我在这里尝试从JSON文件中获取数据:

DATA.json

[
    {
        "Label": "USA",
        "Address": "This is the us",
        "Lat": "36.9628066",
        "Lng": "-122.0194722"
    },
    {
        "Label": "USA",
        "Address": "2020",
        "Lat": "36.9628066",
        "Lng": "-122.0194722"
    }
]

然后将其应用在我的Mainclass中:

using System.Collections.Generic;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using System.IO;
using Newtonsoft.Json;
using System;

namespace Orbage
{
    class MapPage : ContentPage
    {
        public MapPage()
        {
            CustomMap customMap = new CustomMap
            {
                MapType = MapType.Street
            };

            Content = customMap;

            var json = File.ReadAllText("DATA.json");
            var places = JsonConvert.DeserializeObject<List<Place>>(json);
            foreach (var place in places)
            {
                CustomPin pin = new CustomPin
                {
                    Type = PinType.Place,
                    Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
                    Label = place.Label,
                    Address = place.Address,
                    Name = "Xamarin",
                    Url = "http://xamarin.com/about/"
                };

                customMap.CustomPins = new List<CustomPin> { pin };
                customMap.Pins.Add(pin);
                customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
            }
        }
    }
}

但当我将文件放入其中时,它说文件不存在。

错误:

FilenotfoundException

有什么解决方法。
我尝试过:

  • 更改文件的位置。
  • 文件的名称
  • 而不是E:/-/-,我甚至写了DATA.json,但我仍然收到相同的错误。
英文:

I am trying here to consume the data from a JSON file:

DATA.json

[

{&quot;Label&quot;
        
        :&quot;USA&quot;,
        &quot;Adress&quot;:&quot;This is the us&quot;,
        &quot;Lat&quot;:&quot;36.9628066&quot;,
        &quot;Lng&quot;:&quot;-122.0194722&quot;
},
{ &quot;Label&quot;  :&quot;USA&quot;,
         &quot;Address&quot;:&quot;2020&quot;,
          &quot;Lat&quot;:&quot;36.9628066&quot;, 
          &quot;Lng&quot;:&quot;-122.0194722&quot; }

]

Then applying it in my Mainclass:

using System.Collections.Generic;
using Xamarin.Forms.Maps;
using Xamarin.Forms;
using System.IO;
using Newtonsoft.Json;
using System;


namespace Orbage
{
    

class MapPage : ContentPage
    {
        public MapPage()
        {
            CustomMap customMap = new CustomMap
            {
                MapType = MapType.Street

            };
            // ...
            Content = customMap;

            var json = File.ReadAllText(&quot;File.json&quot;);
            var places = JsonConvert.DeserializeObject&lt;List&lt;File&gt;&gt;(json);
            foreach (var place in places)
            {
                CustomPin pin = new CustomPin
                {
                    Type = PinType.Place,
                    Position = new Position(Double.Parse(place.Lat), Double.Parse(place.Lng)),
                    Label = place.Label,
                    Address = place.Address,
                    Name = &quot;Xamarin&quot;,

                    Url = &quot;http://xamarin.com/about/&quot;
                };



                customMap.CustomPins = new List&lt;CustomPin&gt; { pin };

                customMap.Pins.Add(pin);
                customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
            }
        }
    }


}

But when I put the file in there it says that it doesn't exist.

Error:
>FilenotfoundException

Whats the fix for this.
<br>I tried:

  • Changing the location of the file.
  • Its name
    = Instead of E:/-/- I even wrote file.json but I still get the same error.

答案1

得分: 0

你想从 'DATA.json' 读取,但提供了路径 'File.json'。

英文:

You want to read from 'DATA.json' but gave the path 'File.json'.

答案2

得分: 0

问题似乎出在你尝试访问 JSON 数据的方式上。我不确定(因为没有提到),你是将 JSON 存储在哪里,但这里的文档提供了关于如何在使用 Xamarin Forms 的移动手机上访问存储文件的清晰解释。

英文:

Looks like the problem is how you are trying to access your json. I'm not sure(because it doesn't say) as to where are you storing it, but here the docs explain a smooth explanation as to how access stored files in a mobile phone using Xamarin Forms

huangapple
  • 本文由 发表于 2020年8月7日 22:39:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63304004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定