0

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

3
  • 2
    The JSON created by JSON.NET is superior in this case. What is bought by creating an object with one property that is that object serialized? Commented Sep 14, 2011 at 7:40
  • @Tejs +1, I agree with you, but perhaps the OP is bound by the expectations of an external API? Commented Sep 14, 2011 at 7:47
  • You are absolutely right :) I had to make yet another class that represents the member which holds all the properties (being another object) Thank you so much! Do you create an answer? Then I'll gladly accept it. Thanks again! Commented Sep 14, 2011 at 7:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.