英文:
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
[
{"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.
<br>I tried:
- Changing the location of the file.
- Its name
= Instead of E:/-/- I even wrotefile.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论