我有2个类,LatLong
其中包含2个字符串,latitude and longitude
和LatLongs
其中包含list<LatLong>
。C#JSON不能正确使用Newtownsoft.Json解析
public class LatLongs
{
public List<LatLong> latlongs { get; set; }
}
public class LatLong
{
public string latitude { get; set; }
public string longitude { get; set; }
}
我想使用Newtownsoft.Json
JsonConvert.DeserializeObject
功能到我的JSON字符串转换成LatLong
列表。这里是我的JSON
[
{
"latitude":"-34.978113",
"longitude":"138.516192"
},
{
"latitude":"-34.978104",
"longitude":"138.516648"
},
{
"latitude":"-34.978384",
"longitude":"138.516660"
},
{
"latitude":"-34.978398",
"longitude":"138.516225"
}
]
这JSON是有效的,并已检查here
当我尝试使用JsonConvert.DeserializeObject
我得到一个异常
无法反序列化当前的JSON阵列(例如[ 1,2,3])转换为 'SafeAgSystems.Models.LatLongs'类型,因为该类型需要JSON 对象(例如{“name”:“value”})才能正确反序列化。
这里是我使用的尝试将JSON转换成LatLong
List<Locations> coords = await locationTable.Where(u => u.fk_company_id == viewModel.UserData.fk_company_id).ToListAsync();
List<LatLongs> latLongs = new List<LatLongs>();
for (int i = 0; i < coords.Count(); i++)
{
LatLongs latlongs = new LatLongs();
latlongs = JsonConvert.DeserializeObject<LatLongs>(coords[i].geofence_coordinates);
latLongs.Add(latlongs);
}
我已经检查的另一件事列表中的代码是我List<Locations> coords
被填充,这是绝对是。我完全卡住了,我错过了什么?
感谢伊万,你是一个精灵! –