Sachin Kainth
Sachin Kainth

Reputation: 46750

JObject ToObject not Mapping

I have a part of some JSon that I am trying to map to a C# object. The rest of the JSon maps correctly to the object but only this part does not map anything and the objects values are null.

Here is the code that I am calling

var data = JObject.Parse(model.Data);
var dataModel = data.ToObject<MyModel>();

In MyModel I have this code

public class P
            {
                public string R { get; set; }
                public IList<SavedSearch> SavedSearches { get; set; }

                public class SavedSearch
                {
                    public string A { get; set; }
                    public string B { get; set; }
                    public string CD { get; set; }
                }
            }

The corresponding JSon snippet is

"p": [
{
"r": "something",
"savedSearches": [
{
"saved-search": {
"@a": "blah",
"@b": "blahblah",
"@c-d": "blahblahblah"
}
}
]
}
]

R is correctly populated with the correct value, but A, B and CD are not. How must I change my C# model to fix this?

Upvotes: 2

Views: 2440

Answers (1)

dbc
dbc

Reputation: 116805

Your need to add an extra level of indirection for "saved-search", and also inform the serializer how to map the JSON property names to c# property names, since the JSON property names contain invalid characters (e.g. @) for c# properties. Thus:

[DataContract]
public class P
{
    [DataMember(Name="r")]
    public string R { get; set; }

    [DataMember(Name="savedSearches")]
    public IList<SavedSearch> SavedSearches { get; set; }

    [DataContract]
    public class SavedSearch
    {
        [DataMember(Name="saved-search")]
        public SavedSearchItem SavedSearchItem { get; set; }
    }

    [DataContract]
    public class SavedSearchItem
    {
        [DataMember(Name="@a")]
        public string A { get; set; }
        [DataMember(Name = "@b")]
        public string B { get; set; }
        [DataMember(Name = "@c-d")]
        public string CD { get; set; }
    }
}

And

public class MyModel
{
    public List<P> p { get; set; }
}

When used as follows:

        string json = @"
        {""p"": [
        {
        ""r"": ""something"",
        ""savedSearches"": [
        {
        ""saved-search"": {
        ""@a"": ""blah"",
        ""@b"": ""blahblah"",
        ""@c-d"": ""blahblahblah""
        }
        }
        ]
        }
        ]
        }
        ";
        var data = JObject.Parse(json);
        var dataModel = data.ToObject<MyModel>();

        Debug.WriteLine(JsonConvert.SerializeObject(dataModel, Formatting.Indented));

Produce

{
  "p": [
    {
      "r": "something",
      "savedSearches": [
        {
          "saved-search": {
            "@a": "blah",
            "@b": "blahblah",
            "@c-d": "blahblahblah"
          }
        }
      ]
    }
  ]
}

Incidentally, since you will be remapping the property names anyway, I'd suggest using more descriptive names in c# than R, A, B and CD.

Upvotes: 4

Related Questions