简体   繁体   English

使用JsonConvert.SerializeObject的序列化问题(服务器上缺少属性)

[英]Serialization issue using JsonConvert.SerializeObject (properties missing on server)

I am getting pretty weird behaviour using JsonConvert.SerializeObject. 使用JsonConvert.SerializeObject,我的行为越来越奇怪。

I have a method looking like the following: 我有一个方法如下所示:

public class ServiceController : ApiController
{
    public object GetJSONConnectedResponse(object input)
    {
        return JsonConvert.SerializeObject(input, Formatting.Indented,
        new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Serialize
        });
    }


    public object GetMediaGallery(int? id)
    {
        try
        {
            return GetJSONConnectedResponse(GalleryBLL.GetMediaGallery(id));
        }
        catch (Exception exc)
        {
            LogBLL.LogException(exc, HttpContext.Current.Request.RawUrl);
            return null;
        }
    }
}

As you can see, I am using MVC Web API as a service application from which I query ajax-type of data using javascript on the client side. 如您所见,我将MVC Web API用作服务应用程序,通过客户端的javascript从中查询ajax类型的数据。

The method GetMediaGallery returns a holder class of the following structure: 方法GetMediaGallery返回具有以下结构的holder类:

public class MediaGalleryHolder
{
    public List<DB_Image> Images { get; set; }
    public List<string> SpinFrames { get; set; }
    public string FlyoverVideo { get; set; }
}

DB_Images is a complex entity which I populate in a business logic method by calling a stored procedure. DB_Images是一个复杂的实体,我通过调用存储过程以业务逻辑方法填充该实体。 On this complex entity, I have added 2 extra properties. 在这个复杂的实体上,我添加了2个额外的属性。

public partial class DB_Image
{
    public string FullPath { get; set; }
    public string ThumbPath { get; set; }
}

For some reason, on my local machine, the result serializes correctly (adding both the 2 extra properties), but on the server those extra properties are not added to the result (I know they are populated, but never serialized). 出于某种原因,在我的本地计算机上,结果正确地序列化了(同时添加了2个额外的属性),但是在服务器上,那些额外的属性并未添加到结果中(我知道它们已被填充,但是从未序列化)。

Is this some sort of JSON.NET or MVC Web API versioning issue, how can I go about to troubleshoot or resolve this? 这是某种JSON.NET或MVC Web API版本控制问题,如何解决或解决此问题?

Any help would be greatly appreciated! 任何帮助将不胜感激!

Best, 最好,

tribe84 部落84

Have you tried with annotations on each class? 您是否在每个课程上都尝试了注释? you can try adding [Serializable] 您可以尝试添加[可序列化]

  [Serializable] 
  public partial class DB_Image
  {
        public string FullPath { get; set; }
        public string ThumbPath { get; set; }
  }

  [Serializable]
  public class MediaGalleryHolder
  {
       public List<DB_Image> Images { get; set; }
       public List<string> SpinFrames { get; set; }
       public string FlyoverVideo { get; set; }
  }

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JsonConvert.SerializeObject C#时JSON结果中的问题 - Issue In JSON Result when using JsonConvert.SerializeObject C# 使用 JSON.net(JsonConvert.SerializeObject 或 JsonConvert.DeSerializeObject)为缺少的复杂属性设置默认值 - Set Default value for missing Complex properties with JSON.net (JsonConvert.SerializeObject or JsonConvert.DeSerializeObject) 使用JsonConvert.SerializeObject时发生可枚举错误 - Error for enumerable when using JsonConvert.SerializeObject 使用JsonConvert.SerializeObject序列化Tweetinvi - Serialize Tweetinvi using JsonConvert.SerializeObject 使用JsonConvert.SerializeObject创建Json结构 - Create Json structure using JsonConvert.SerializeObject JsonConvert.SerializeObject使用非空DateTime属性进行分类? - JsonConvert.SerializeObject to class with non-nullable DateTime properties? JsonConvert.SerializeObject 将子级转换为父级仍然返回父级属性 - JsonConvert.SerializeObject with casted child to parent still returns parent properties 使用JsonConvert.SerializeObject()时,将base64解析到服务器在Json中返回null。 - parsing base64 to server returns null in Json when using JsonConvert.SerializeObject(); JsonConvert.SerializeObject转义反斜杠 - JsonConvert.SerializeObject escaping backslash 使用JsonConvert.SerializeObject的json.net在输出中添加注释 - json.net using JsonConvert.SerializeObject adds a comment to output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM