0

The below JSON and C# object works but it is UGLY c# object and with 50 states, it looks more ugly and not manageable. Looking for cleaner way to create c# object and that generates the same JSON. I can't modify the JSON because it is defined by the plugin.This website http://json2csharp.com/ is generating the below class. The application doesn't support the latest c# language

    {
      "main_settings": {
        "width": "700",
        "background_color": "#FFFFFF",  
        "fade_time": 0.1,
        "link_text": "View Website"
      },
      "state_specific": {
        "AB": {
          "name": "Alberta",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        },
        "AK": {
          "name": "Alaska",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        },
        "AL": {
          "name": "Alabama",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        },
        "AR": {
          "name": "Arkansas",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        },
        "AZ": {
          "name": "Arizona",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        },

        "YT": {
          "name": "Yukon",
          "description": "default",
          "color": "default",
          "hover_color": "default",
          "url": "default"
        }
      }

    }

C# class - to create the same JSON. May be better way to handle lists and arrays

    public class MainSettings
    {
        public string width { get; set; }
        public string background_color { get; set; }
        public double fade_time { get; set; }
        public string link_text { get; set; }
    }

    public class AB
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class AK
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class AL
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class AR
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class AZ
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class YT
    {
        public string name { get; set; }
        public string description { get; set; }
        public string color { get; set; }
        public string hover_color { get; set; }
        public string url { get; set; }
    }

    public class StateSpecific
    {
        public AB AB { get; set; }
        public AK AK { get; set; }
        public AL AL { get; set; }
        public AR AR { get; set; }
        public AZ AZ { get; set; }
        public YT YT { get; set; }
    }

    public class RootObject
    {
        public MainSettings main_settings { get; set; }
        public StateSpecific state_specific { get; set; }
    }
SNew
  • 109
  • 1
  • 2
  • 10
  • 3
    I've not read the question, but it's probably a duplicate of [this](https://stackoverflow.com/questions/24771804/how-can-i-deserialize-a-child-object-with-dynamic-numeric-key-names) – ProgrammingLlama Mar 27 '20 at 03:30
  • 1
    I agree, this question is essentially a duplicate. You don't have an array here, you have a dictionary. Create a single class `State` to replace all your individual state classes (`AB`, `AK`, etc.). Then change the type of the `state_specific` property in your `RootObject` to `Dictionary` and get rid of the `StateSpecific` class. – Brian Rogers Mar 27 '20 at 04:40
  • @BrianRogers Can you please a post a sample code , I am using this website to generate the classes http://json2csharp.com/ The application doesn't support the latest c# language – SNew Mar 27 '20 at 11:56
  • @BrianRogers I figured your referred article, it solves serialization problem but it is not deserializing to the state two-digit code as shown in the above JSON – SNew Mar 27 '20 at 13:17
  • 1
    @SNew Here is full a [round-trip demo](https://dotnetfiddle.net/XOfJ5f) using the dictionary approach. It works fine for me. – Brian Rogers Mar 27 '20 at 17:42
  • @BrianRogers Thank you very much, I understood my error and cleaned ugly json2csharp.com generated code as per your suggestion. One quick question calling after DeserializeObject, I would like to access a state like Alaska by it's name "Alaska" to modify its values like color, description, and SerializeObject the same object again to pass it back to the client. Trying to figure LinQ – SNew Mar 27 '20 at 19:41
  • @SNew I would recommend [opening a new question](https://stackoverflow.com/questions/ask) for that; I'm sure it will be straightforward to help with that once we see what you are trying to do. If you need to, you can link your new question back to this one to help provide context. – Brian Rogers Mar 27 '20 at 20:58

0 Answers0