Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Json.NET返回空对象序列化Android.Gms.Maps.Model.LatLng对象列表_C#_Json_Google Maps_Xamarin_Json.net - Fatal编程技术网

C# Json.NET返回空对象序列化Android.Gms.Maps.Model.LatLng对象列表

C# Json.NET返回空对象序列化Android.Gms.Maps.Model.LatLng对象列表,c#,json,google-maps,xamarin,json.net,C#,Json,Google Maps,Xamarin,Json.net,序列化MapRoute对象时,我得到了JSON如下数据: “{\'RouteName\':\'route1\',\'RouteWaypoints\':[{},{},{},{},{},{}]}” 路径点列表未正确序列化 using Android.Gms.Maps.Model; using Newtonsoft.Json; using System.Collections.Generic; namespace App3.Model { public class Id { [Js

序列化
MapRoute
对象时,我得到了
JSON
如下数据:

“{\'RouteName\':\'route1\',\'RouteWaypoints\':[{},{},{},{},{},{}]}”

路径点列表未正确序列化

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; }
  }
}

我相信
Android.Gms.Maps.Model.LatLng
实际上是。可能(我自己无法测试)Json.NET的反射机制无法成功发现此类包装器类型的成员

如果是这样,最简单的解决方法是引入一个,将
LatLng
映射到DTO,然后序列化它。首先,按如下方式定义您的类型:

// Required for For System.Linq.Enumerable.Select<TSource,TResult>
// https://developer.xamarin.com/api/member/System.Linq.Enumerable.Select%7BTSource,TResult%7D/p/System.Collections.Generic.IEnumerable%7BTSource%7D/System.Func%7BTSource,TResult%7D/
using System.Linq;  

public class LatLngDTO
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public static implicit operator LatLng(LatLngDTO latLng)
    {
        if (latLng == null)
            return null;
        return new LatLng(latLng.Latitude, latLng.Longitude);
    }

    public static implicit operator LatLngDTO(LatLng latLng)
    {
        if (latLng == null)
            return null;
        return new LatLngDTO { Latitude = latLng.Latitude, Longitude = latLng.Longitude };
    }
}

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<LatLngDTO> RouteWaypoints { get; set; }
}
_mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => (LatLngDTO)l).ToList());

我修改了@dbc LatLngDTO类并编写了两个静态对象返回器方法,如下所示:


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);
            }
        }
它解决了我的问题。我用LatLngDTO对象列表序列化了我的模型,如下所示:

mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => LatLngDTO.ToLatLngDTO(l)).ToList());

            string json = JsonConvert.SerializeObject(mapRoute);
            Json = json;
            http.PostHTTPData(urlString, json);

你说它没有正确序列化-为什么?它是抛出异常还是不填充?可能是因为它错误地序列化了基础Java.Lang.Object-可能值得尝试实现一个模型类并序列化它。@Hesen您可以共享您的LatLng类定义吗?根据您的屏幕截图,这不是您正在使用的serialize命令。您还传递了一些JsonSerializerSettings。这些设置是什么?另外,你能展示你设置航路点的代码吗,这样我们就可以看到它们里面有什么了?@JesusAngulo-LatLng是Android.Gms.Maps.Model库的一个类,我不是自己写的。它包含当前位置的纬度和经度。我尝试了您的解决方案变体。首先,它毫无例外地将我从调试模式中抛出,然后我做了一些更改。我真的不记得现在它在
公共静态隐式操作符latlngdo(LatLng LatLng){if(LatLng==null)中循环返回null;返回新的LatLng(LatLng.Latitude,LatLng.Longitude);}
方法。我把@HesenTagiyev放在那里-抱歉,我没有对代码进行单元测试就回答了。修正了。@Hesentagiev不将此标记为答案有点粗鲁,因为您自己的答案完全基于此one@Mick-我的最初有一个错误,因为已更正。赫森塔吉耶夫的回答也纠正了这一点,尽管方式不同。@dbc我特别强调,我发现它是基于你的回答。再次感谢你们的回答,它解决了80%的问题。
mapRoute.RouteWaypoints = (_wayPoints == null ? null : _wayPoints.Select(l => LatLngDTO.ToLatLngDTO(l)).ToList());

            string json = JsonConvert.SerializeObject(mapRoute);
            Json = json;
            http.PostHTTPData(urlString, json);