tRuEsAtM
tRuEsAtM

Reputation: 3668

How to convert a JSON response of a particular type into another JSON response?

I have an existing JSON response which looks like,

{"rarautomation":{"stable":{"ixRepo":"100024"}},"crmweb":{"stable":{"ixRepo":"100028"},"release":{"ixRepo":"101543"}},"models":{"stable":{"ixRepo":"100341"},"PhaseOutDefaultModel":{"ixRepo":"102088"},"FfwModelUpdate2017Q4":{"ixRepo":"102258"},"SsiQ42017":{"ixRepo":"102266"}}}

I have written a c# code to get the new JSON response which is like,

var repoList = new Dictionary<string, List<string>>();

tempList.Add("master");

tempList.Add("release");

repoList["IdentifyApplicationDataService"] = tempList;

I return this dictionary as JSON response, which looks like

"IdentifyApplicationDataService":["master","release"],"CallLogger":["master"],"UniversalPackagingSystem":["master"]}

How should I modify my C# representation to get a response like my 1st JSON response?

Upvotes: 0

Views: 42

Answers (1)

Toan Nguyen
Toan Nguyen

Reputation: 11591

You can model you class like this

public class IxData
        {
            public string IxRepo { get; set; }
        }

        public class DeviceData
        {
            public Dictionary<string, IxData> Models { get; set; }
            public Dictionary<string, IxData> CrmWeb { get; set; }
            public Dictionary<string, IxData> Rarautomation { get; set; }
        }

and use it like this

 var device = new DeviceData
            {

                CrmWeb = new Dictionary<string, IxData>
                {
                    {
                        "stable", new IxData
                        {
                            IxRepo = "100028"
                        }
                    },
                    {
                        "release", new IxData
                        {
                            IxRepo = "101543"
                        }
                    }
                },
                Rarautomation = new Dictionary<string, IxData>
                {
                    {
                        "stable", new IxData
                        {

                            IxRepo = "100024"
                        }
                    }
                },
                Models = new Dictionary<string, IxData>
                {
                    {
                        "stable", new IxData
                        {
                            IxRepo = "100341"
                        }
                    },
                    {
                        "PhaseOutDefaultModel", new IxData
                        {
                            IxRepo = "102088"
                        }
                    }
                },
            };

            var json = JsonConvert.SerializeObject(device, new JsonSerializerSettings()
            {

                Formatting =  Formatting.Indented,
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

The output is enter image description here

Upvotes: 1

Related Questions