我有一个数组数组作为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 = Coordinatesso 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();更多推荐
发布评论