4

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?

1
  • Create a DTO with the structure / field naming you want then use that with json.net. Commented Jul 20, 2012 at 16:13

4 Answers 4

3

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
    });
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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} } }
}

Comments

0
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; }
}

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.