2

I have been given a JSON definition something along the lines of..

{
    "the.data" : {
        "first.name": "Joe",
        "last.name": "Smith"
    }
}

and i've made a class in c# to add my data too, such as

public class TheData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class RootObject
{
    public TheData TheData { get; set; }
}

Now the web service that i have to send this payload to, is expecting The.Data, and First.Name, as well as Last.Name

How can i 'change' the name definitions? Before transmitting?

Can i somehow override the name?

m1nkeh
  • 1,337
  • 23
  • 45
  • 1
    Which JSON API are you using? (I suspect you just want `[JsonProperty("first.name")`] etc.) – Jon Skeet Oct 02 '15 at 09:46
  • if yuo are using Json.Net you cas use `[JsonProperty(PropertyName = "name")]` attribute. – xZ6a33YaYEfmv Oct 02 '15 at 09:46
  • Possible duplicate of [How can I change property names when serializing with Json.net?](http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net) – NibblyPig Oct 02 '15 at 09:48

2 Answers2

3

You can try this. you can use JsonPropertyAttribute to tell Json.Net what the property's corresponding json field is.

public class TheData
{
    [JsonProperty("first.name")]
    public string FirstName { get; set; }
    [JsonProperty("last.name")]
    public string LastName { get; set; }
}

public class RootObject
{
    [JsonProperty("the.data")]
    public TheData TheData { get; set; }
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

You can decorate the values, it will depend on which framework you're using but it'll be something like this:

[JsonProperty(PropertyName = "Person.Name")]
public string PersonName { get; set; }
NibblyPig
  • 51,118
  • 72
  • 200
  • 356