在 C# 中将两个 JSON 结构合二为一

我们有两个相似但略有不同的 JSON 结构。第一个 JSON 结构适用于 No Strata 场景——这适用于 measureId “001” 和 “002” 。第二个 JSON 结构用于多层 – 这适用于“measureId”:“003”。对于每个 testTIN,我们需要将这两个测量值结合起来,如预期的 JSON 结构所示。


JSON 1 – 无分层


{

    "testTIN": "123",

    "measurements": [{

            "measureId": "001",

            "value": {

                "IsEndToEndReported": true,

                "PerformanceMet": 5

            }

        },

        {

            "measureId": "002",

            "value": {

                "IsEndToEndReported": true,

                "PerformanceMet": 6

            }


        }

    ]

}

JSON 2 – 多层


{

    "testTIN": "123",

    "measurements": [

       {

        "measureId": "003",

        "value": {

            "strata": [{

                    "IsEndToEndReported": true,

                    "PerformanceMet": 5,

                    "Stratum": "Level1"

                },

                {

                    "IsEndToEndReported": true,

                    "PerformanceMet": 6,

                    "Stratum": "Level2"

                }

            ]

        }

    }

   ]

}

预期的 JSON


{

    "testTIN": "123",

    "measurements": [{

            "measureId": "001",

            "value": {

                "IsEndToEndReported": true,

                "PerformanceMet": 5

            }

        },

        {

            "measureId": "002",

            "value": {

                "IsEndToEndReported": true,

                "PerformanceMet": 6

            }

        },

        {

            "measureId": "003",

            "value": {

                "strata": [{

                        "IsEndToEndReported": true,

                        "PerformanceMet": 5,

                        "Stratum": "Level1"

                    },

                    {

                        "IsEndToEndReported": true,

                        "PerformanceMet": 6,

                        "Stratum": "Level2"

                    }

                ]

            }

        }

    ]

}

如何获得结合上述两种 JSON 结构的新 JSON 结构?


月关宝盒
浏览 491回答 2
2回答

素胚勾勒不出你

您可以简单地进行联合(除非您确实需要,否则无需创建复杂的 POCO 类)。Newtonsoft 支持合并 JSon 的:var dataObject1 = JObject.Parse(@"{            ""testTIN"" : ""123"",            ""measurements"": [{                ""measureId"": ""001"",                ""value"": {                    ""IsEndToEndReported"": true,                    ""PerformanceMet"": 5                }            },            {                ""measureId"": ""002"",                ""value"": {                    ""IsEndToEndReported"": true,                    ""PerformanceMet"": 6                }            }            ]        }");        var dataObject2 = JObject.Parse(@"{        ""testTIN"": ""123"",        ""measurements"": [        {            ""measureId"": ""003"",            ""value"": {                ""strata"": [{                    ""IsEndToEndReported"": true,                    ""PerformanceMet"": 5,                    ""Stratum"": ""Level1""                },                {                    ""IsEndToEndReported"": true,                    ""PerformanceMet"": 6,                    ""Stratum"": ""Level2""                }                ]            }        }        ]        }");        dataObject1.Merge(dataObject2, new JsonMergeSettings        {            // union array values together to avoid duplicates            MergeArrayHandling = MergeArrayHandling.Union        });        string json = dataObject1.ToString();这将给出一个输出:    {  "testTIN": "123",  "measurements": [    {      "measureId": "001",      "value": {        "IsEndToEndReported": true,        "PerformanceMet": 5      }    },    {      "measureId": "002",      "value": {        "IsEndToEndReported": true,        "PerformanceMet": 6      }    },    {      "measureId": "003",      "value": {        "strata": [          {            "IsEndToEndReported": true,            "PerformanceMet": 5,            "Stratum": "Level1"          },          {            "IsEndToEndReported": true,            "PerformanceMet": 6,            "Stratum": "Level2"          }        ]      }    }  ]}

慕容708150

如果您的初始No Strata和多层测量结果实际上已经序列化为JSON 结构JContainer.Merge(Object, JsonMergeSettings),您可以使用合并设置将它们简单地合并在一起MergeArrayHandling.Concat,如下所示:// Get the initial measurement JSON measurements as strings.IEnumerable<string> measturements = GetJsonMeasurements();// And concatenate them together into a combined `JObject`:var settings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat };var json = measturements.Aggregate(new JObject(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(j, s) => { j.Merge(JObject.Parse(s), settings); return j; });在这里,我假设测量值已经按"testTIN"值分组。如果没有,这很容易通过在聚合之前将所有结果解析为 aJObject并按值分组来添加"testTIN",如下所示:var settings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat };var json = measurements&nbsp; &nbsp; .Select(j => JObject.Parse(j))&nbsp; &nbsp; .GroupBy(j => (string)j["testTIN"])&nbsp; &nbsp; .Select(g => g.Aggregate(new JObject(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(j1, j2) => { j1.Merge(j2, settings); return j1; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; .ToList();或者,如果您的 JSON 结果存储在某些文件集合中,您可以直接从文件中合并,如下所示:var settings = new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat };var json = fileNames.Aggregate(new JObject(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(j, s) =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;using (var file = File.OpenText(s))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;using (var reader = new JsonTextReader(file))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j.Merge(JToken.Load(reader), settings);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return j;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});演示在这里摆弄单元测试。更新如果您有一个可枚举的对象并希望通过将它们的 JSON 表示合并在一起来创建一个组合的 JSON 文件,您可以将每个对象投影到JObjectusing JObject.FromObject,然后合并它们:// Get the resultsIEnumerable<MeasurementSet__NoStrata> measurements1 = GetNoStrataMeasurements(); // Get no-strata measurements.IEnumerable<MeasurementSet__MultiStrata> measurements2 = GetMultiStrataMeasurements(); // Get multistrata measurements.// Combine them into a single enumerableIEnumerable<object> measurements = measurements1.Cast<object>()&nbsp; &nbsp; .Concat(measurements2.Cast<object>());// Select serialization and merge settingsvar serializerSettings = new JsonSerializerSettings{&nbsp; &nbsp; // Put whatever you want here, e.g.&nbsp; &nbsp; NullValueHandling = NullValueHandling.Ignore,};var mergeSettings = new JsonMergeSettings&nbsp;{&nbsp;&nbsp; &nbsp; // Required&nbsp; &nbsp; MergeArrayHandling = MergeArrayHandling.Concat,&nbsp; &nbsp; // Put whatever you want here, either Ignore or Merge&nbsp; &nbsp; // https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_MergeNullValueHandling.htm&nbsp; &nbsp; MergeNullValueHandling = MergeNullValueHandling.Ignore, // Or Merge if you prefer};// Serialize and merge the resultsvar serializer = JsonSerializer.CreateDefault(serializerSettings);var json = measurements&nbsp; &nbsp; .Select(m => JObject.FromObject(m, serializer))&nbsp; &nbsp; .GroupBy(j => (string)j["testTIN"])&nbsp; &nbsp; .Select(g => g.Aggregate(new JObject(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(j1, j2) => { j1.Merge(j2, mergeSettings); return j1; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; // Do we need to remove the `"category"` property?&nbsp; &nbsp; // If so do it here.&nbsp; &nbsp; .Select(o => { o.Remove("category"); return o; })&nbsp; &nbsp; .ToList();演示小提琴#2在这里。
打开App,查看更多内容
随时随地看视频慕课网APP