JSON.Net串行忽视JsonProperty? [英] JSON.Net serializer ignoring JsonProperty?

查看:319
本文介绍了JSON.Net串行忽视JsonProperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的实体类:

public class FacebookComment : BaseEntity
{
    [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
    [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
    [JsonProperty("_id")]
    public ObjectId Id { get; set; }

    public int? OriginalId { get; set; }
    public DateTime Date { get; set; }
    public string Message { get; set; }
    public string Sentiment { get; set; }
    public string Author { get; set; }
}

在此对象序列化到JSON,我想Id字段是写成_id:{...}。据我所知,我应该只需要通过所需的PROPERTYNAME到JsonProperty属性;我应该是好去。然而,当我打电话JsonConvert.SerializeObject;它似乎忽略了我的属性,使得这个代替:

When this object is serialized to JSON, I want the Id field to be written as "_id":{...}. AFAIK, I should just have to pass the desired propertyname to the JsonProperty attribute; and I should be good to go. However, when I call JsonConvert.SerializeObject; it seems to ignore my attribute and renders this instead:

{
    Author: "Author name",
    Date: "/Date(1321419600000-0500)/",
    DateCreated: "/Date(1323294923176-0500)/",
    Id: {
        CreationTime: "/Date(0)/",
        Increment: 0,
        Machine: 0,
        Pid: 0,
        Timestamp: 0
    },
    Message: "i like stuff",
    OriginalId: null,
    Sentiment: "Positive"
}

正如你所看到的,ID字段被渲染了错误的字段名。

As you can see, the Id field is being rendered with the wrong field name.

任何想法?在这一问题上了一个小时的工作;想不通为什么串行器似乎忽略了我的JsonProperty。

Any ideas? Been working on this issue for an hour; can't figure out why the serializer is seemingly ignoring my JsonProperty.

任何有建设性的意见是极大的赞赏。

Any constructive input is greatly appreciated.

推荐答案

我通过我的标记与System.Runtime.Serialization.DataMember名称=)Id属性修正了这个问题。还没完全清楚,为什么它没有原来的工作,虽然...

I fixed this issue by marking my Id property with [System.Runtime.Serialization.DataMember(Name="_id")] instead of JsonProperty. Still not entirely clear as to why it didn't work originally though...

这篇关于JSON.Net串行忽视JsonProperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆