如何将特定类型的JSON响应转换为另一个JSON响应?

时间:2018-07-26 23:46:19

标签: c# json

我有一个现成的JSON响应,

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

我已经编写了一个c#代码来获取新的JSON响应

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

tempList.Add("master");

tempList.Add("release");

repoList["IdentifyApplicationDataService"] = tempList;

我将此字典作为JSON响应返回,看起来像

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

我应该如何修改C#表示形式以获得类似于第一个JSON响应的响应?

1 个答案:

答案 0 :(得分:1)

您可以像这样对您的课程进行建模

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

并像这样使用它

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

输出为 enter image description here