m1nkeh
m1nkeh

Reputation: 1397

JSON Class with spaces in name

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?

Upvotes: 2

Views: 3258

Answers (2)

NibblyPig
NibblyPig

Reputation: 52932

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

Upvotes: 0

Mohit S
Mohit S

Reputation: 14034

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

Upvotes: 3

Related Questions