bomortensen
bomortensen

Reputation: 3396

Json.NET Serializing my object gives me the wrong output

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

Upvotes: 0

Views: 1391

Answers (1)

Tejs
Tejs

Reputation: 41236

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).

Upvotes: 1

Related Questions