r3plica
r3plica

Reputation: 13387

JArray to c# Object

I have this json string:

[
   {
      "id":"EORDERING_GRE017",
      "name":"DELIMITER",
      "value":"|"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"ENABLED",
      "value":"Y"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"EXTERNALERRORRECIPIENT",
      "value":"[email protected]; [email protected]"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"FILETYPE",
      "value":"delimited"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"INTERNALERRORRECIPIENT",
      "value":"[email protected]; [email protected]"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"USESOWNBRANCHCODES",
      "value":"True"
   },
   {
      "id":"EORDERING_GRE017",
      "name":"USESOWNSKUS",
      "value":"True"
   }
]

And I would like to turn that json into my class, which looks like this:

public class Settings
{
    public bool Enabled { get; set; }
    public string FileType { get; set; }
    public string Delimiter { get; set; }
    public string OrderFileSuffix { get; set; }
    public string ResultFileSuffix { get; set; }
    public bool UseOwnBranchCodes { get; set; }
    public bool UseOwnProductCodes { get; set; }
    public string InternalContacts { get; set; }
    public string ExternalContacts { get; set; }
}

But I am unsure which is the best way to do this. Can someone give me a hand? :)

Upvotes: 0

Views: 2098

Answers (3)

Fabjan
Fabjan

Reputation: 13676

You could create a NameValue object:

public class NameValuePair
{
   public string Name { get; set; }
   public string Value { get; set; }
}

And deserialize the json array to List and convert it to Dictionary:

var dict = JsonConvert.DeserializeObject<List<NameValuePair>>(json).ToDictionary(x => x.Name, x => x.Value);

Then create a custom converter class with a method that accepts this dictionary and returns a Settings object:

public class SettingsConverter
{
    public Settings Convert(IDictionary<string, string> data)
    {
        return new Settings
        {
            Enabled = data["ENABLED"].Equals("Y", StringComparison.Ordinal),
            ...
        };
    }
}

Upvotes: 3

Arvind Sisara
Arvind Sisara

Reputation: 861

For those out there that may need some more help with the JSON Class Configuration, try: http://json2csharp.com/#

An excellent way of Auto Generating the Classes!

Or even easier, in VS, Goto:

Edit -> Paste Special -> Paste as JSON Classes

Upvotes: 0

smile
smile

Reputation: 78

The JSON is an array of objects that would fit this class:

public class Setting
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
}

So deserialize into that:

var settingList = JsonConvert.DeserializeObject<Setting[]>(jsonString);

But then you want to map specific settings to specific properties of that Settings class. You could do that by trying to find the particular setting for each property in the list of settings:

var settingsObject = new Settings
{
    FileType = settingList.FirstOrDefault(s => s.Name == "FILETYPE")?.Value,
    Delimiter = settingList.FirstOrDefault(s => s.Name == "DELIMITER")?.Value,
    // ...
}

You'll have to manually map the properties. If you don't want that, you could write a custom serializer, but that'll quickly become a maintenance nightmare.

Upvotes: 3

Related Questions