1

I am reading Json files and populate List of Items using Newtonsoft.Json. I have a problem reading some Json files were the name of the attribute has spaces and therefore it is not the same as the attributes of Item class.

This is fine. Item object will assigned with the below values.

[
    {
        "FirstName": "John",
        "SecondName": "Smith"
    }
]

But my Json file is like this thus Item object has null values for FirstName and SecondName

[
    {
        "First Name": "John",
        "Second Name": "Smith"
    }
]

Therefore my Item which has properties of FirstName and SecondName (see below) will get a null value.

public class Item
{

    private string firstName = "";
    private string secondName = "";

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string SecondName
    {
        get { return secondName; }
        set { secondName = value; }
    }
}

Is there a simple way how to solve this problem. I have no control on the namings (e.g. FirstName or First Name in the Json string.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
user2307236
  • 665
  • 4
  • 19
  • 44

2 Answers2

1

This should get the job done.

[DataContract]
public class Item
{
    private string firstName= "";
    private string secondName= "";

    [DataMember(Name = "First Name")]
    public string FirstName
    {
        get { return firstName; }
        set { firstName= value; }
    }

    [DataMember(Name = "Second Name")]
    public string SecondName
    {
        get { return secondName; }
        set { secondName= value; }
    }
}

If this isn't flexible enough for your needs you will have to implement custom formatting.
Read more about it here: WebApi Json.NET custom date handling

Community
  • 1
  • 1
Mihai Dinculescu
  • 19,743
  • 8
  • 55
  • 70
  • That'll work for the files that *do* have the spaces, but of course, not for the ones that don't (OP seems to have to handle both). – T.J. Crowder Nov 29 '14 at 12:13
  • In that case he will have to implement custom formatting as I have suggested. – Mihai Dinculescu Nov 29 '14 at 12:15
  • what if it doesn't get the job done? what else should I look for? in my case the "First Name" looks like this below, is it important that it is not a primitive type?... "Parent": { "First Name": { "Child1": [ 1, 2, 3 ], "Child2": [ 1, 2, 3 ] } } – ovi Aug 10 '17 at 22:11
1

If you're using Json.NET, you can use the JsonPropertyAttribute to specify the name of your properties in JSON:

public class Item
{

    private string firstName = "";
    private string secondName = "";

    [JsonProperty("First Name")]
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    [JsonProperty("Second Name")]
    public string SecondName
    {
        get { return secondName; }
        set { secondName = value; }
    }
}
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
  • 1
    This is effectively the same answer as @Mihai gave and has the same problem, as T.J Crowder pointed out-- it works for JSON files that do have spaces, but not for the ones that don't, and the OP needs to be able to handle both. – Brian Rogers Nov 29 '14 at 18:08