如何修复文件 .json 找不到的问题,当它确实存在时。

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

how to fix file .json not found when it is there

问题

我正在尝试从一个 JSON 文件中获取数据,但当我把文件放在那里时,它显示文件不存在。错误:FilenotfoundException。有什么解决方法。我尝试过:更改文件位置、文件名,甚至写成 file.json,但仍然出现相同的错误。非常感谢!

注: 请注意,您提供的代码中包含了 HTML 编码的字符,我已经将它们还原为正常文本以便更好地理解。如果您需要更多帮助,可以提供详细的代码和文件路径信息。

英文:

I am trying here to consume the data from a json file:
DATA.json

[

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

]

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("File.json");
            var places = JsonConvert.DeserializeObject<List<File>>(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)));
            }
        }
    }


}

But when I put the file in there it says that it doesn't exist.
Error:
FilenotfoundException
Whats the fix for this.
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.
Thanks alot!

答案1

得分: 2

在读取文件之前,最好先检查文件是否存在于指定位置。

if (File.Exists("File.Json"))

通过这样做,您可以确定您提供的路径是否正确或不正确。

英文:

Its better before reading the file, put check whether file exists in the location or not.

if (File.Exists("File.Json"))

With this you would become to know that your given path is correct or not.

答案2

得分: 2

尝试使用以下方法:

将你的 File.json 放入你的 PCL 项目中,并将其 Build Action 设置为 EmbeddedResource

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MapPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("Orbage.File.json");
string text = "";
using (var reader = new System.IO.StreamReader(stream))
{
    text = reader.ReadToEnd();
}
英文:

Try to use the method below:

Put your File.json to your PCL project,and set its Build Action to EmbeddedResource.

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MapPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("Orbage.File.json");
string text = "";
using (var reader = new System.IO.StreamReader(stream))
     {
       text = reader.ReadToEnd();
     }

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

发表评论

匿名网友

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

确定