Kevin Pilch
Kevin Pilch

Reputation: 11605

Deserialize flat JSON data into structure with JSon.net

I'm consuming a web service that returns JSON data, and in numerous cases the services returns several properties in one object that I would like to group into a class on the C# side. Consider a class structure like:

  class Person
  {
      public Address Address { get; set; }
      public string Name {  get; set; }
  }

  class Address
  {
      public string StreetAddress { get; set; }
      public string City {  get; set; }
      public string ZipCode {  get; set; }
  }

And JSON data like:

{ "Name" : "Pilchie",
"StreetAddress" : "1234 Random St",
"City" : "Nowheretown",
"Zip" : "12345"
}

Is it possible to attribute my Person and Address classes so that they serialize/deserialize into this format?

Upvotes: 4

Views: 2079

Answers (4)

L.B
L.B

Reputation: 116098

var person = JsonConvert.DeserializeObject<Person>(json);

class Person
{
    [JsonProperty("StreetAddress")]
    private string _StreetAddress { get; set; }

    [JsonProperty("City")]
    private string _City { get; set; }

    [JsonProperty("Zip")]
    private string _ZipCode { get; set; }

    public string Name { get; set; }

    public Address Address
    {
        get
        {
            return new Address() { City = _City, StreetAddress = _StreetAddress, ZipCode = _ZipCode };
        }
    }
}

class Address
{
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
}

Upvotes: 0

Peter Ritchie
Peter Ritchie

Reputation: 35881

I don't think you can get JSON.NET to do it all in one shot--You'll have to create the Person object manually. But, you can do it without creating separate DTO classes. For example:

var jsonText = "{ \"Name\" : \"Pilchie\"," +
            "\"StreetAddress\" : \"1234 Random St\"," +
            "\"City\" : \"Nowheretown\"," +
            "\"Zip\" : \"12345\"" +
            "}";
JObject jsonObject = (JObject) JsonConvert.DeserializeObject(jsonText);

var person =
    new Person
    {
        Address = new Address
                    {
                    City = (String) jsonObject["City"],
                    StreetAddress = (String) jsonObject["StreetAddress"],
                    ZipCode = (string) jsonObject["Zip"]
                    },
        Name = (string) jsonObject["Name"]
    };

And serializing:

JsonConvert.SerializeObject(
    new
    {
        person.Name,
        person.Address.StreetAddress,
        person.Address.City,
        Zip = person.Address.ZipCode
    });

Upvotes: 3

Jon
Jon

Reputation: 2891

It all depends on how you will use the data. You could do something like this if you just want access to an address property:

class Person
{
    public string Name { get; set; }
    public string StreetAddress { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }

    [ScriptIgnore]
    public Address Address { get {return new Address(){StreetAddress = this.StreetAddress,
                                                       City = this.City,
                                                       ZipCode = this.ZipCode} } }
}

Upvotes: 0

Justin Rusbatch
Justin Rusbatch

Reputation: 4052

I think the simplest approach to deserializing the data you're consuming would be to create a simple DTO object that matches the format of the JSON data. You could then easily map the data to the new structure using AutoMapper, or a similar library.

Upvotes: 0

Related Questions