Json.NET 序列化我的对象给了我错误的输出

发布于 2024-12-04 06:16:42 字数 2753 浏览 1 评论 0原文

我在使用 JSON.NET 将 .NET 对象序列化为 JSON 时遇到了一些问题。我想要序列化的输出必须如下所示:

{"members":[

    {"member":
        {
            "id":"4282",
            "status":"1931",
            "aktiv":"1",
            "firmanavn":"firmname1",
            "firmaUrl":"www.firmurl.dk",
            "firmaUrlAlternativ":"",
            "firmaSidstKontrolleretDato":"30-08-2010",
            "firmaGodkendelsesDato":"07-03-2002"
        }
    },
    {"member":
        {
            "id":"4283",
            "status":"1931",
            "aktiv":"1",
            "firmanavn":"firmname2",
            "firmaUrl":"www.firmurl.dk",
            "firmaUrlAlternativ":"",
            "firmaSidstKontrolleretDato":"30-08-2010",
            "firmaGodkendelsesDato":"18-12-2000"
         }
},
      ...... long list of members omitted

我现在的 .NET 结构(仍在尝试获得正确的输出)如下所示:

public class Members
{
    public List<Member> MemberList { get; set; }
}

以及:

public class Member
{
    [JsonProperty(PropertyName = "Id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "Status")]
    public string Status { get; set; }

    [JsonProperty(PropertyName = "Aktiv")]
    public string Aktiv { get; set; }

    [JsonProperty(PropertyName = "Firmanavn")]
    public string Firmanavn { get; set; }

    [JsonProperty(PropertyName = "FirmaUrl")]
    public string FirmaUrl { get; set; }

    [JsonProperty(PropertyName = "AltFirmaUrl")]
    public string AlternativFirmaUrl { get; set; }

    [JsonProperty(PropertyName = "FirmaSidstKontrolleretDato")]
    public string FirmaSidstKontrolleretDato { get; set; }

    [JsonProperty(PropertyName = "FirmaGodkendelsesDato")]
    public string FirmaGodkendelsesDato { get; set; }
}

调用时上面的 .NET 结构给了我什么:

string json = JsonConvert.SerializeObject(members, Newtonsoft.Json.Formatting.Indented);

Where 'members'是成员列表。是这样的:

{
  "Members": [
   {
      "Id": "1062",
      "Status": "1933",
      "Aktiv": "1",
      "Firmanavn": "firmname",
      "FirmaUrl": "http://www.firmurl.dk",
      "AltFirmaUrl": "http://www.altfirmurl.dk",
      "FirmaSidstKontrolleretDato": "13-09-2011",
      "FirmaGodkendelsesDato": "13-09-2511"
   },
   {
      "Id": "1060",
      "Status": "1933",
      "Aktiv": "1",
      "Firmanavn": "firmname2",
      "FirmaUrl": "http://www.firmurl.dk",
      "AltFirmaUrl": "http://www.altfirmurldk",
      "FirmaSidstKontrolleretDato": "13-09-2011",
      "FirmaGodkendelsesDato": "13-09-2511"
   },

所以基本上,结构是正确的,因为它按预期创建了成员数组,但我缺少每个成员对象上的“member”:标签。有没有办法制作这样的标签?某种类声明,或者其他什么?

我希望我的问题很清楚,如果不是 - 请告诉我,我会尽力进一步解释。

预先非常感谢。

/ 博

I'm having a bit of a problem with serializing my .NET objects into JSON using JSON.NET. The output I want to be serialized will have to look like this:

{"members":[

    {"member":
        {
            "id":"4282",
            "status":"1931",
            "aktiv":"1",
            "firmanavn":"firmname1",
            "firmaUrl":"www.firmurl.dk",
            "firmaUrlAlternativ":"",
            "firmaSidstKontrolleretDato":"30-08-2010",
            "firmaGodkendelsesDato":"07-03-2002"
        }
    },
    {"member":
        {
            "id":"4283",
            "status":"1931",
            "aktiv":"1",
            "firmanavn":"firmname2",
            "firmaUrl":"www.firmurl.dk",
            "firmaUrlAlternativ":"",
            "firmaSidstKontrolleretDato":"30-08-2010",
            "firmaGodkendelsesDato":"18-12-2000"
         }
},
      ...... long list of members omitted

My .NET structure for now (still experimenting to get the right output) is like this:

public class Members
{
    public List<Member> MemberList { get; set; }
}

and:

public class Member
{
    [JsonProperty(PropertyName = "Id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "Status")]
    public string Status { get; set; }

    [JsonProperty(PropertyName = "Aktiv")]
    public string Aktiv { get; set; }

    [JsonProperty(PropertyName = "Firmanavn")]
    public string Firmanavn { get; set; }

    [JsonProperty(PropertyName = "FirmaUrl")]
    public string FirmaUrl { get; set; }

    [JsonProperty(PropertyName = "AltFirmaUrl")]
    public string AlternativFirmaUrl { get; set; }

    [JsonProperty(PropertyName = "FirmaSidstKontrolleretDato")]
    public string FirmaSidstKontrolleretDato { get; set; }

    [JsonProperty(PropertyName = "FirmaGodkendelsesDato")]
    public string FirmaGodkendelsesDato { get; set; }
}

What the above .NET structure gives me when calling:

string json = JsonConvert.SerializeObject(members, Newtonsoft.Json.Formatting.Indented);

Where 'members' is a list of members. Is this:

{
  "Members": [
   {
      "Id": "1062",
      "Status": "1933",
      "Aktiv": "1",
      "Firmanavn": "firmname",
      "FirmaUrl": "http://www.firmurl.dk",
      "AltFirmaUrl": "http://www.altfirmurl.dk",
      "FirmaSidstKontrolleretDato": "13-09-2011",
      "FirmaGodkendelsesDato": "13-09-2511"
   },
   {
      "Id": "1060",
      "Status": "1933",
      "Aktiv": "1",
      "Firmanavn": "firmname2",
      "FirmaUrl": "http://www.firmurl.dk",
      "AltFirmaUrl": "http://www.altfirmurldk",
      "FirmaSidstKontrolleretDato": "13-09-2011",
      "FirmaGodkendelsesDato": "13-09-2511"
   },

So basically, the structure is right in that it creates the array of members as expected, but I am missing the "member": label on each of the member objects. Is there any way to make such a label? Some kind of class declaration, or something?

I hope my question is clear, if not - please let me know and I'll try to explain further.

Thanks a lot in advance.

/ Bo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2024-12-11 06:16:42

听起来您只需要创建一个具有该名称属性的中间对象即可获取您想要的 JSON;但是,我会考虑使用 JSON.net 最初渲染的 JSON(因为它在结构上更好)。

It sounds like you just need to make an intermediary object with a property of that name to get the JSON you want; however, I'd consider using the JSON.net originally rendered JSON (as it is structurally better).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文