Json.NET返回序列化Android.Gms.Maps.Model.LatLng对象列表的空对象 [英] Json.NET returns empty objects serializing list of Android.Gms.Maps.Model.LatLng objects
问题描述
在序列化 MapRoute
对象时,我得到了 JSON
这样的数据:
{\RouteName \:\route1 \,\RouteWaypoints \:[{},{},{},{} ,{}]}
RouteWayPoints
列表未正确序列化。 / p>
使用Android.Gms.Maps.Model;
使用Newtonsoft.Json;
使用System.Collections.Generic;
namespace App3.Model
{
public class Id
{
[JsonProperty(PropertyName =$ oid)]
public string id {get;组; }
}
public class MapRoute
{
public Id _id {get;组; }
public string RouteName {get;组; }
public List< LatLng> RouteWaypoints {get;组; }
}
}
这里 LatLng
是 <$ c $来自 Android.Gms.Maps.Model.LatLng
。
序列化命令:
$
string json = JsonConvert.SerializeObject(mapRoute);
使用Android.Gms.Maps.Model;
namespace App3.Model
{
public class LatLngDTO
{
public double Latitude {get;组; }
public double Longitude {get;组; }
public LatLngDTO(double lat,double lng)
{
Latitude = lat;
经度= lng;
public static LatLng ToLatLng(LatLngDTO latLng)
{
if(latLng == null)
return null;
else
返回新的LatLng(latLng.Latitude,latLng.Longitude);
public static LatLngDTO ToLatLngDTO(LatLng latLng)
{
if(latLng == null)
return null;
else
返回新的LatLngDTO(latLng.Latitude,latLng.Longitude);
}
}
解决了我的问题。我使用LatLngDTO对象列表序列化了我的模型,如下所示:
mapRoute.RouteWaypoints =(_wayPoints == null?null:_wayPoints 。选择(l => LatLngDTO.ToLatLngDTO(l))。ToList());
string json = JsonConvert.SerializeObject(mapRoute);
Json = json;
http.PostHTTPData(urlString,json);
While serializing MapRoute
object I got JSON
data like this:
"{\"RouteName\":\"route1\",\"RouteWaypoints\":[{},{},{},{},{}] }"
RouteWayPoints
list are not serialized properly.
using Android.Gms.Maps.Model;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace App3.Model
{
public class Id
{
[JsonProperty(PropertyName = "$oid")]
public string id { get; set; }
}
public class MapRoute
{
public Id _id { get; set; }
public string RouteName { get; set; }
public List<LatLng> RouteWaypoints { get; set; }
}
}
Here LatLng
is Android.Gms.Maps.Model.LatLng
from the Google Maps API for Xamarin.Android.
Serialize command:
string json = JsonConvert.SerializeObject(mapRoute);
i modified @dbc LatLngDTO class and write two static object returner methods like below:
using Android.Gms.Maps.Model;
namespace App3.Model
{
public class LatLngDTO
{
public double Latitude { get; set; }
public double Longitude { get; set; }
public LatLngDTO(double lat,double lng)
{
Latitude = lat;
Longitude = lng;
}
public static LatLng ToLatLng(LatLngDTO latLng)
{
if (latLng == null)
return null;
else
return new LatLng(latLng.Latitude, latLng.Longitude);
}
public static LatLngDTO ToLatLngDTO(LatLng latLng)
{
if (latLng == null)
return null;
else
return new LatLngDTO(latLng.Latitude, latLng.Longitude);
}
}
it solved my issue. And i serialized my model with list of LatLngDTO objects like below:
mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => LatLngDTO.ToLatLngDTO(l)).ToList());
string json = JsonConvert.SerializeObject(mapRoute);
Json = json;
http.PostHTTPData(urlString, json);
这篇关于Json.NET返回序列化Android.Gms.Maps.Model.LatLng对象列表的空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!