如何处理数组中的数组?(How do I process arrays within an array?)

编程入门 行业动态 更新时间:2024-10-27 07:32:08
如何处理数组中的数组?(How do I process arrays within an array?)

我有一个数组数组作为JSON数组,数组包含在下面的地方

F1 = Feature #1 P1 = Point #1 X / Y = Coordinates

所以F1P1X是特征#1的#1点的X值。

[ [ [F1P1X,F1P1Y,null], [F1PnX,F1PnY,null] ], [ [F2P1X,F2P1Y,null], [F2PnX,F2PnY,null] ], [ [FnP1X,FnP1Y,null], [FnPnX,FnPnY,null] ] ]

这里是我用来从文件中获取上述JSON的代码:

string json = File.ReadAllText("ABC.json"); JObject obj = JObject.Parse(json); JToken token = obj.SelectToken("$.features[?(@.name == 'X')]['XY']"); var paths = JToken.Parse(token.ToString()).SelectToken("XYZ");

接下来,我需要使用各种阵列来构建字符串。 我如何获得第二级数组(特征),以便我可以处理它最内层的数组(点上的特征)? 最后将是List<string> ,其中每个字符串都是一个要素(JSON中的二级数组),而最内层的数组是指制作该要素的点。 我可以处理字符串操作,但首先我需要从JSON中获取数组。

I have an array of arrays of arrays as JSON, a sample included below where

F1 = Feature #1 P1 = Point #1 X / Y = Coordinates

so F1P1X is the X-value of point #1 of feature #1.

[ [ [F1P1X,F1P1Y,null], [F1PnX,F1PnY,null] ], [ [F2P1X,F2P1Y,null], [F2PnX,F2PnY,null] ], [ [FnP1X,FnP1Y,null], [FnPnX,FnPnY,null] ] ]

Here is the code I use to get the above JSON from a file:

string json = File.ReadAllText("ABC.json"); JObject obj = JObject.Parse(json); JToken token = obj.SelectToken("$.features[?(@.name == 'X')]['XY']"); var paths = JToken.Parse(token.ToString()).SelectToken("XYZ");

Next, I need to build strings using the various arrays. How do I get the second-level array (the feature) so I can process its innermost arrays (points on the features)? The end will be List<string> where each string is a feature (second-level array in the JSON) and the innermost array are the points that make the feature. I can handle the string manipulation but first I need to get the arrays out of the JSON.

最满意答案

JSON.NET nuget包是JSON的良好选择。 我为你创建了测试方法。

//Read your json file string json = File.ReadAllText("ABC.json"); //deserialize F1P1X[][][] yourArrayOfArraysOfArrays = JsonConvert.DeserializeObject<F1P1X[][][]>(json).ToList(); public class F1P1X { public string Feature { get; set; } public string Point { get; set; } public string Coordinates { get; set; } } public static void Test() { F1P1X[] test1Array = new[] { new F1P1X { Feature = "F1", Point = "P1", Coordinates = "X1" }, new F1P1X { Feature = "F2", Point = "P2", Coordinates = "X2" }, }; F1P1X[] test2Array = new[] { new F1P1X { Feature = "F3", Point = "P3", Coordinates = "X3" }, new F1P1X { Feature = "F4", Point = "P4", Coordinates = "X4" }, }; F1P1X[][] test = {test1Array, test2Array}; F1P1X[][] test2 = { test1Array, test2Array }; F1P1X[][][] top = {test, test2}; //array of arrays of arrays as JSON string json = JsonConvert.SerializeObject(top); List<F1P1X[][]> arrays = JsonConvert.DeserializeObject<F1P1X[][][]>(json).ToList(); foreach (F1P1X[][] item in arrays) { foreach (F1P1X[] f1P1X in item) { foreach (F1P1X p1X in f1P1X) { //do some magic } } } // or use linq var result = arrays.SelectMany(x => x.SelectMany(y => y.Where(z => z.Coordinates == "X1"))); }

这个linq语句返回要素列表

List<string> result = arrays.SelectMany(x => x.SelectMany(y => y.Select(z => z.Feature))).ToList();

Good choice is Json.NET nuget package for work with JSON. I have create test method for you.

//Read your json file string json = File.ReadAllText("ABC.json"); //deserialize F1P1X[][][] yourArrayOfArraysOfArrays = JsonConvert.DeserializeObject<F1P1X[][][]>(json).ToList(); public class F1P1X { public string Feature { get; set; } public string Point { get; set; } public string Coordinates { get; set; } } public static void Test() { F1P1X[] test1Array = new[] { new F1P1X { Feature = "F1", Point = "P1", Coordinates = "X1" }, new F1P1X { Feature = "F2", Point = "P2", Coordinates = "X2" }, }; F1P1X[] test2Array = new[] { new F1P1X { Feature = "F3", Point = "P3", Coordinates = "X3" }, new F1P1X { Feature = "F4", Point = "P4", Coordinates = "X4" }, }; F1P1X[][] test = {test1Array, test2Array}; F1P1X[][] test2 = { test1Array, test2Array }; F1P1X[][][] top = {test, test2}; //array of arrays of arrays as JSON string json = JsonConvert.SerializeObject(top); List<F1P1X[][]> arrays = JsonConvert.DeserializeObject<F1P1X[][][]>(json).ToList(); foreach (F1P1X[][] item in arrays) { foreach (F1P1X[] f1P1X in item) { foreach (F1P1X p1X in f1P1X) { //do some magic } } } // or use linq var result = arrays.SelectMany(x => x.SelectMany(y => y.Where(z => z.Coordinates == "X1"))); }

This linq statement returns list of features

List<string> result = arrays.SelectMany(x => x.SelectMany(y => y.Select(z => z.Feature))).ToList();

更多推荐

本文发布于:2023-07-24 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何处理   组中   array   process

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!